Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 4 An Architecture to Suit You

scheduler.ScheduleAction(

TimeSpan.FromSeconds(1), () =>

{

SetTime(DateTime.Now);

});

}

}

}

Now that you have added a load of code, let’s summarize what you have done.

•\

You have created a new class named ClockWidget.

•\

You have defined your state type as Clock.

•\

You have initialized (known as init in the MVU

 

pattern) your model field clock.

•\

You have defined your view with the body() function.

•\

You have defined your update function in the form of

 

the SetTime method.

Note that there are two common scenarios when an update is called: when there is user interaction (e.g., a click/tap of a button) and around asynchronous background work. Your example here applies to the second scenario.

XAML vs. C# Markup

XAML has proven to be a big part of building application UIs in Xamarin. Forms and it will likely continue in .NET MAUI, but I want to make it clear that you do not have to use it. So, if like some friends and colleagues, the verbosity of XAML makes you feel queasy, there is a solution!

86

Chapter 4 An Architecture to Suit You

Anything that you can create in XAML can ultimately be created in C#. Furthermore, there are ways to improve on the readability of the C# required to build UIs.

Some benefits of building user interfaces solely with C# are

•\

A single file for a view. No pairing of .xaml.cs and

 

.xaml files.

•\

Better refactoring options so renaming properties or

 

commands in XAML won’t update the C#.

Let’s work through how you can build your ClockWidget in C# in all its verbosity and then I will show how you can simplify it using C# Markup. (I must add this is an open-source package that you need to bring in). Also, these examples are still built using MVVM.

Plain C#

As mentioned, anything you can build in XAML can also be built in C#. The following code shows how the exact same XAML definition of your ClockWidget can be built using just C#:

using WidgetBoard.ViewModels;

namespace WidgetBoard.Views;

public class ClockWidget : ContentView

{

public ClockWidget()

{

BindingContext = new ClockWidgetViewModel();

var label = new Label

{

FontSize = 80,

87

Chapter 4 An Architecture to Suit You

HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center

};

label.SetBinding(

Label.TextProperty,

nameof(ClockWidgetViewModel.Time));

Content = label;

}

}

The code above does the following things:

•\

Creates a single file representing your ClockWidget

•\

Points your widget’s BindingContext to the

 

ClockWidgetViewModel

•\

Creates a label and set its Text property to be bound to

 

the view models Time property

•\

Assigns the label to the content of the view

C# Markup

I have recently come to appreciate the value of being able to fluently build UIs. I don’t tend to do it often because I personally feel comfortable building with XAML or perhaps it is Stockholm Syndrome kicking in (I’ve been working with XAML for well over 10 years now). When I do, it needs to be as easy to read and build as possible given it is not something I do often.

88