You have already learned a lot from this book, but to truly learn, you have to practice. In this chapter, you will build a couple of projects.
Blazor UI bindings
Local storage in Blazor
Event handling in Blazor
Task 1
Your first task will include several simple exercises to practice the general syntax of Blazor, as well as a more complex exercise where you will need to use components and local storage.
Description
You will create a Blazor client-side application that will allow you to make calculations according to the instructions provided. The application will include the following attributes:
Age calculator: An age calculator simply allows you to enter two dates and return the difference in years.
Cylinder surface area: Use the following formula:
A = 2πrh + 2πr^2
A = area
r = radius
h = height
This will allow you to calculate all the variables from the rest of them.
Rectangular area: Use the following formula:
A = a * b
A = area
a = side a
b = side b
This will allow you to calculate all the variables from the rest of them.
This app will allow the calculations to be saved locally for later use.
Trapezoid area calculator: Use the following formula:
A = (a + b) / 2 * h
A = area
a = base 1
b = base 2
h = height
This will allow you to calculate all the variables from the rest of them.
Area of triangle calculator: Use the following formula:
A = (h * b) / 2
A = area
h = height
b = base length
This will allow you to calculate all the variables from the rest of them.
Rectangular area calculator: Use the following formula:
A = a * b
A = area
a = side a
b = side b
- 1.
Create calculator pages based on the formulas provided.
- 2.
Add a local save feature for the calculation made for the rectangular area.
Solution
Our solution will be separated into several parts, for each part of description. As always, this is just one of many possible solutions rather than the only one.
Age Calculator Solution
Let’s create the age calculator in this section.
Age Calculator Page
The more interesting part of this is how we execute the Calculate method . The task does not have any specific requirements, but you can either create a simple button and execute it on a click/tap or do something more exciting like we have here. On a change of the value, we assign the new value to the variable, and with that, we also execute calculations. This is a good quick way to handle more than one operation in a single event.
Cylinder Surface Area Calculator
Cylinder Surface Area Calculator Page
The trick in this task is to put things in proper places and not make a mess of things, such as having the same formula for two different outputs. First, we display our formula in basic text format, and then we also have our formula with input fields in it. For each variable, we have different methods that calculate a value, and for each of them, we have different buttons that execute those methods.
Trapezoid Area Calculator
Trapezoid Area Calculator Page
Triangle Area Calculator
Triangle Area Calculator Page
Rectangle Area Calculator
Calculation History Data Model
Calculation History Item Component
Calculator Page
The calculation for rectangular variables is the same as others; we have variables set through input boxes, and then on submit the calculation is made based on formulas, in the methods Calculate_A, Calculate_b, and Calculate_a.
The difference here is that those methods also contain a check for a Boolean: savecalculation. It can be set to true or false using the checkbox, and if it is true, the calculation is saved. To save the calculation, the result data is serialized into a JSON string, which then is inserted into local storage. Notice that for the ID ticks of the current UTC date are used. Also, the ID can be reused as a date stamp for when the result was saved. Alternatively, the GUID could be used. Then, for reading the records, the system simply retrieves the JSON string from local storage and deserializes that to the result list.
Navigation Page
Task 2
For this task, we’ll build an invoice generator. The invoice is basically a written request from one business to another for a payment. The invoice generator should state the company details, items, values, and total values. Our version will be simplified.
Description
Since our invoice is simplified, we will have only a couple of company details, and the biggest part of the development will be a sales item. The user should be able to add as many items as they want, and the total each time should be added to the total of the invoice.
ID
Description
Total (generated from items)
Description
Price
Tax
Total
In the sales items, you also need to provide the total for each item. Use a component for a sales item. Note that you are not required to do any actual PDF, PNG, or other visual outputs of the invoice.
Solution
Just like the previous task , this is not the only solution possible, but your project will be similar.
Main Layout
Invoice Model (InvoiceModel.cs)
Sales Item Model (SalesItemModel.cs)
Main Page (Index.razor)
Sales Item Component (SalesItem.razor)
While the invoice page and items component may seem complex, when you look closely, they use only the most basic features of Blazor. The most difficult part here is dealing with the components and attempting to calculate the total when there are changes. The component (Listing 7-13) simply takes an ID, because when it gets generated, all the values are empty. The most important parts here are the callbacks; as you can see, all the input fields have one, and they all act differently. The description is the simplest one, as it only returns the ID and the new description value. The tax and value are more complex; we first need to establish methods, which will calculate the values in the component and display them in the component directly. Then, these methods invoke our callbacks; to get further, we need to switch to the page. Our page (shown in Listing 7-12) handles the callbacks differently, but for the most part, the idea is to assign the values to the list of items, because that is what would be generated for some kind of visual format.
Summary
Both of these tasks not only give you an opportunity to practice your skills but also showed you how client-side Blazor can make your business more efficient. For any of these tasks, there is absolutely no need to go to the server side, which saves you a lot of money. Using components simplifies development and keeps your code cleaner.