You’ll need to obtain the rest of the software from within Visual Studio, so let’s open that next. If the installation went as expected, the launch dialog will be onthe screen:
- Click onCreate a new project. (If you’ve been brought directly into Visual Studio, bypassing the launch dialog, just clickFile |New Project.)
- It is now time to pick the template we want. Templates make getting started easier. In theSearch for Templates box, enter
MAUI
. A few choices will be presented; you want.NET MAUI App, as shown in thefollowing figure:
Figure 1.4 – Creating a new project
- ClickNext. Herewe give our project a name. The first project we’re going to create is not
ForgetMeNotDemo
(the project that you will be building as part of this book), but rather a sample project just to take a quick look around. Name it something creative such asSampleApp
and place it in a location on your disk where you will be able to easily find it later. Before clickingNext, make sure your dialog looks similar toFigure 1.5.
Figure 1.5 – Naming your project
- ClickNext and usethe dropdown to choose the latest version of .NET. At the time of writing, that is .NET 7. Finally, clickonCreate.
Note
Because Microsoft is always updating Visual Studio, your screens or steps may vary slightly. Don’t let that worry you. The version I am using is Visual Studio 2022, version 17.4.3. As long as yours is the same or later, you’re all set. But just to be sure, let’s launch the sample app (F5). You should see something that looks likeFigure 1.6.
Figure 1.6 – Running your app
- On the screen that you see in the preceding figure, click the button a couple times to make sure itis working.
Generally speaking, I will not be walking through how to do simple things on Visual Studio. The assumption is that you are a C# programmer and so you are probably familiar with Visual Studio. On the other hand, on the off chance that you are not, I’ll describe how to do anything that is not immediately intuitive. Next, let’s explore the out-of-the-box app in a bitmore detail.
Quick tour of the app
Let’s take a quick tourto see what comes with an out-of-the-box app. First, stop the app by pressing the red square button in the menu bar. Make sureSolution Explorer is open (if not, go toView |Solution Explorer). Notice that there are three folders and four files, as shown inFigure 1.7:
Figure 1.7 – Three folders and four files
The files with the.xaml
extension are XAML files – that is, they use the XAML markup language. I will not assume you know XAML, and in fact, throughout this book, I will provide layout and other code in both XAML and fluent C#, but that is for thenext chapter.
Right now, let’s openthis out of the
box project
.
This is the entry point for the program. As you can see, it is a static class with a static method that is responsible for creating the app itself. We’ll come back to this file insubsequent chapters.
When you openMainPage.xaml
, you will see a layout with controls for the page we just looked at (with the goofy MAUI guy waving and counting our button clicks). Again, we’re going to come back to layout and controls, but scan this page and see whether you can guess what is going on. You may find that it isn’t as alien as it seemed at first glance. You can, if you are so motivated, learn quite a bit about XAML just by reading thispage carefully.
Click on the triangle next toMainPage.xaml
to reveal the code-behind file. Code-behind files are always named<PageName>.xaml.cs
– in this case,MainPage.xaml.cs
. These files are always in C#. Here, we see the constructor and then an event handler. When the user clicks on the button, this event handler (OnCounterClicked
)is called.
By flipping back and forth between the XAML and the code-behind file, you may be able to figure out how the button works and how the count of clicks is displayed. No need to do this, however, as we’ll be covering all these details inupcoming chapters.
At the moment, most of the other files are nearly empty and not worth the timeto examine.
Just for fun, expand theResources
folder. You’ll see that there are folders for the application icon, fonts, images, and so forth. All the resources for all of the platforms arekept here.
Then there is aPlatforms
folder, which contains whatever is needed on a per-platform basis. For example, iOS applications require aninfo.plist
file, which you’ll find inPlatforms |iOS.
There is much more to see in a .NET MAUI application, but we will tackle each part as we build ForgetMe Not™.