Reader small image

You're reading from  .NET MAUI Projects - Third Edition

Product typeBook
Published inFeb 2024
Reading LevelN/a
PublisherPackt
ISBN-139781837634910
Edition3rd Edition
Languages
Right arrow
Authors (3):
Michael Cummings
Michael Cummings
author image
Michael Cummings

Michael Cummings is a Senior Development Engineer with Microsoft. He currently works on the Visual Studio tooling for building WPF, Xamarin.Forms, and .NET MAUI apps. He has experience as a developer and architect with concentrations in app development, design, deployment, and business process as it relates to technology. Michael has been a graphics and game programmer since the days of the TI99-4/A. He started the Boston XNA Developers Group. He has contributed to many open-source projects, including AXIOM, a .NET/Mono 3D rendering engine, and Planeshift, a 3D MMORPG. Michael also has experience with native game and graphics development (C++/DirectX) on Windows. Michael lives in Lexington, MA with his wife and their dog. When not working with technology he enjoys watching movies, trying out new recipes, and the occasional game of full-contact racquetball.
Read more about Michael Cummings

Daniel Hindrikes
Daniel Hindrikes
author image
Daniel Hindrikes

Daniel Hindrikes is a developer and architect with a passion for developing mobile apps powered by the cloud. Daniel fell in love with Xamarin in the early days of Xamarin when he realized that he could use C# even for iOS and Android apps, and that he could share code with the Windows apps he was also building. But Daniel started to build mobile apps long before that, working on Android apps with Java and even Java ME apps (a long, long time ago). Daniel enjoys sharing his knowledge and can be found speaking at conferences, blogging, or recording the podcast, The Code Behind. Daniel works at the company tretton37 in Sweden and has experience working with both local and global customers.
Read more about Daniel Hindrikes

Johan Karlsson
Johan Karlsson
author image
Johan Karlsson

Johan Karlsson has been working with Xamarin since the days of MonoTouch and Mono for Android, and it all started with writing a game. He is a full-stack developer, currently focusing on mobile applications using Xamarin, but has in the past worked a lot with ASP.NET MVC, Visual Basic.NET (not proud), and C# in general. Also, he's created a whole bunch of databases in SQL Server over the years. Johan works at tretton37 in Sweden and has about 20 years' experience in the trade of assembling ones and zeros.
Read more about Johan Karlsson

View More author details
Right arrow

Building a Calculator Using .NET MAUI Blazor

In this chapter, we will explore a .NET Blazor app embedded within a .NET MAUI app. We will write part of the calculator app using Blazor and host that in .NET MAUI using BlazorWebView. We will also communicate between Blazor and .NET MAUI.

In this chapter, we are going to cover the following topics:

  • What is Blazor?
  • Exploring the differences between .NET MAUI projects and .NET MAUI Blazor projects
  • Using HTML and CSS to define a UI
  • Using XAML controls with HTML in a WebView
  • Writing C# code that will integrate with XAML controls and HTML controls
  • Working with the main .NET MAUI window to resize it to its contents

Technical requirements

You will need to have Visual Studio for Mac or PC installed, as well as the .NET MAUI components. See Chapter 1, Introduction to .NET MAUI, for more details on how to set up your environment. The source code for this chapter is available in this book’s GitHub repository: https://github.com/PacktPublishing/MAUI-Projects-3rd-Edition.

Project overview

In this chapter, you will discover .NET Blazor and how to use it with .NET MAUI to develop a UI for an app. We will explore the different options available for hosting a Blazor app within a .NET MAUI app. Communication between the two apps is a crucial part of the interoperability and this chapter’s project will show you how to send data from .NET MAUI to Blazor and vice versa.

What is Blazor?

.NET Blazor is a web framework built on top of .NET. Blazor applications run in the browser by using WebAssembly (WASM), or on a server using SignalR. Blazor is one part of the whole ASP.NET ecosystem, and it leverages Razor pages for developing the UI. Blazor uses HTML and CSS to render a rich UI. Blazor uses a component-based UI, where each component is a Razor markup page. Within a Razor page, you can mix and match HTML, CSS, and C# code. There are three deployment models for a Blazor app:

  • Blazor Server: In a Blazor Server deployment, the application code runs on the server in an ASP.NET Core app, and it communicates with the UI running in a browser via SignalR.
  • Blazor WebAssembly: For Blazor WebAssembly, the entire app runs in the browser via WASM. It is an open web standard that makes it possible to run .NET code safely in the browser. WASM provides interoperability with JavaScript.
  • Blazor Hybrid: Blazor Hybrid is a blend of native .NET and web...

Creating the calculator app

In this chapter, we are going to build a calculator app. The UI for the calculator is built using Razor pages in Blazor, but the actual mechanics of the calculator reside in the .NET MAUI app.

Setting up the project

This project, like all the rest, is a File | New | Project...-style project. This means that we will not be importing any code at all. So, this first section is all about creating the project and setting up the basic project structure.

Creating the new project

The first step is to create a new .NET MAUI project. Follow these steps:

  1. Open Visual Studio 2022 and select Create a new project:
Figure 11.1 – Visual Studio 2022

Figure 11.1 – Visual Studio 2022

This will open the Create a new project wizard.

  1. In the search field, type in blazor and select the .NET MAUI Blazor App item from the list:
Figure 11.2 – Create a new project

Figure 11.2 – Create a new project

  1. Click Next.
  2. Enter Calculator as the...

Summary

Excellent work! In this chapter, you completed a project that used the .NET MAUI Blazor template. You created a UI using HTML, updated it with C# code, and then implemented a service that was managed by .NET MAUI and injected it into a Razor page. Then, you used CollectionView to display a list of the previous calculations. Within CollectionView ItemTemplate, the SwipeView control was used to recall a previous calculation to the keypad for additional editing and reevaluation.

To extend this project even further, consider the following:

  • Add an additional swipe action to delete a calculation from the collection
  • Add an additional keypad layout for scientific calculations

In the next – and final – chapter, you will discover the world of AI as you build an object recognition app.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
.NET MAUI Projects - Third Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781837634910
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Michael Cummings

Michael Cummings is a Senior Development Engineer with Microsoft. He currently works on the Visual Studio tooling for building WPF, Xamarin.Forms, and .NET MAUI apps. He has experience as a developer and architect with concentrations in app development, design, deployment, and business process as it relates to technology. Michael has been a graphics and game programmer since the days of the TI99-4/A. He started the Boston XNA Developers Group. He has contributed to many open-source projects, including AXIOM, a .NET/Mono 3D rendering engine, and Planeshift, a 3D MMORPG. Michael also has experience with native game and graphics development (C++/DirectX) on Windows. Michael lives in Lexington, MA with his wife and their dog. When not working with technology he enjoys watching movies, trying out new recipes, and the occasional game of full-contact racquetball.
Read more about Michael Cummings

author image
Daniel Hindrikes

Daniel Hindrikes is a developer and architect with a passion for developing mobile apps powered by the cloud. Daniel fell in love with Xamarin in the early days of Xamarin when he realized that he could use C# even for iOS and Android apps, and that he could share code with the Windows apps he was also building. But Daniel started to build mobile apps long before that, working on Android apps with Java and even Java ME apps (a long, long time ago). Daniel enjoys sharing his knowledge and can be found speaking at conferences, blogging, or recording the podcast, The Code Behind. Daniel works at the company tretton37 in Sweden and has experience working with both local and global customers.
Read more about Daniel Hindrikes

author image
Johan Karlsson

Johan Karlsson has been working with Xamarin since the days of MonoTouch and Mono for Android, and it all started with writing a game. He is a full-stack developer, currently focusing on mobile applications using Xamarin, but has in the past worked a lot with ASP.NET MVC, Visual Basic.NET (not proud), and C# in general. Also, he's created a whole bunch of databases in SQL Server over the years. Johan works at tretton37 in Sweden and has about 20 years' experience in the trade of assembling ones and zeros.
Read more about Johan Karlsson