Bill Wagner creates C# learning and reference materials for https://learn.microsoft.com. He works with colleagues on the C# team, related content teams, and customers to provide resources for everyone that wants to learn more about C#. He’s also a member of the ECMA C# Standardization committee, working to update the C# standard.
I asked Bill, to come talk about C# 11 and to do so from the perspective of a programmer who stopped following the newest additions to C# as of C# 7 or 8 or so. That is, how do we catch up if we’ve fallen behind on the latest features?
This is part one, in which we discuss, among other things, Pattern Matching.
Mark Price joined me to discuss C# 11 and his books, C# 11 and .NET 7 and Apps and Services with .NET 7. Both books are available in my tiny book store, listed under Favorite books for programmers.
Note: A gremlin got into the original audio; fixed now. I apologize for the inconvenience.
Picking up where we left off, I want to add unit tests to my program. Now, I know, I should have been using unit tests all along. I have no excuse and hang my head in shame.
To get started, I’ve decided to add xUnit through NuGet. Easy peasy. But it won’t work. The full explanation is here. Great article. Here, in a nutshell are the steps you need to perform:
Create an xUnit test project in the same solution as ForgetMeNot
Make sure it runs
Add a reference to the ForgetMeNot project
This wil not run yet. Here is where the fun begins
First, open up the ForgetMeNot project and change the TargetFrameworks to
Building on the previous postings, today I want to discuss the magic of Dependency Injection (DI)
Dependency Injection makes for cleaner and more testable code. We’ll get into testing and Mocks in a later blog post, but using DI allows you to substitute a mock service for a real one. In any case, it is magical.
Even though this is powerful, this posting will be short, because once you know how to do it, it is very easy.
Let’s look at BuddyPreferences. This is the page that lists all the preferences of a specific buddy. We start, in BuddyPreferences.xaml.cs by creating BuddyPreferencesViewModel so that it can be our BindingContext. We pass the ViewModel into the constructor. Here is the complete class:
public partial class BuddyPreferences : ContentPage
{
private BuddyPreferencesViewModel vm;
public BuddyPreferences(BuddyPreferencesViewModel vm)
{
this.vm = vm;
BindingContext = vm;
InitializeComponent();
}
}
Building on the previous blog posts, here I’d like to illustrate how you can pass complex data from one page’s view model to another’s.
Let’s assume we’ve tapped on the Buddies Icon on the tab bar and were taken to the BuddyList:
If we tap on one of our buddies, in this case, Rodrigo, we should be taken to the details page for that buddy. We could pass along the buddy’s id, or we can pass the buddy object itself.
This is part 4 in an ongoing series in which I will build and dissect a non-trivial app. For details, please see the first in this series.
Part 3 ended with a teaser about the Preferences Page. As you’ll remember, we start with the Preference Model class:
namespace ForgetMeNot.Model;
[ObservableObject]
public partial class Preference
{
[ObservableProperty] private string preferencePrompt;
[ObservableProperty] private string preferenceValue;
}
I explained about ObservableProperties, so I won’t review that here. Let’s see how this model class is used. There are two important related classes: Preferences.xaml and PreferencesViewModel.cs
In the previous postings we looked at creating the basic app and adding a single, simple page. This post will really begin to get into it.
We’re going to have a number of pages
A page for you to enter your own preferences
One for looking at your buddy list
One for look at your buddy’s preferences
A page for inviting someone to become your buddy
Each of these pages will have its own viewmodel class. We’ll also need services to talk to the API client (e.g., BuddyService with methods to get your buddy list, add to your list, see your buddy’s preferences, etc.)
In Part 1 we created the skeleton of Forget Me Not (and explained what it is). Here in Part 2 we’ll add an about page. This is so easy that this will be a short post.
Create the page
Creating the page is almost indistinguishable from doing so in Xamarin.Forms. Here’s the XAML for the page:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="ForgetMeNot.About"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<VerticalStackLayout Margin="10" Spacing="10">
<HorizontalStackLayout Spacing="10">
<Image HeightRequest="64" Source="flower.png" />
<Label
FontAttributes="Bold"
FontSize="22"
Text="About this app"
VerticalOptions="End" />
<Label
FontSize="22"
Text="v1.0"
VerticalOptions="End" />
</HorizontalStackLayout>
<Label Text="This app is written in XAML and C# with .NET MAUI by Jesse Liberty and Rodrigo Juarez. Concept and original design by Robin Liberty" />
</VerticalStackLayout>
</ContentPage>
We put a picture of a forget-me-not flower next to the title. Below that is the version and the acknowledgements. We’ll want to be able to navigate to the About page using a tab at the bottom of the page, so let’s turn to AppShell.xaml and ad the tab there: