5

Shorter Development Times With the .NET MAUI Community Toolkit

 1 year ago
source link: https://devm.io/dotnet/dotnet-maui-community-toolkit-dev
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

.NET MAUI: An Overview - Part 1

Shorter Development Times With the .NET MAUI Community Toolkit


The easiest way to reuse source code across projects is to copy it from one project to another. Although this isn’t the best way, it’s often used in cross-platform app development with .NET MAUI for small helper functions, since creating a separate NuGet package for a single helper function would be too costly. The .NET MAUI Community Toolkit solves this problem by providing a set of common helper functions in a shared NuGet package.

The .NET MAUI Community Toolkit is a library developed by the open source community with a variety of common helper functions and components for building .NET MAUI apps. It is hosted on GitHub and is the successor to the Xamarin Community Toolkit. Although it’s a community project and not an official Microsoft product, it is maintained primarily by Microsoft employees. A notable feature of the toolkit is that, unlike other community projects, documentation isn’t available on GitHub, but on the Microsoft homepage. With this move, Microsoft has emphasized the project’s importance for .NET MAUI development.

The .NET MAUI Community Toolkit’s components fall into the following categories, which we’ll look at in more detail in the following sections:

  • Alerts
  • Animations
  • Behaviors
  • Converters
  • Extensions
  • ImageSources
  • Layouts
  • Views

A Guide for the .NET MAUI Community Toolkit’s Components

Alerts

Alerts include the snackbar and toast. Both are used to display small message areas on the screen. But unlike the toast, the snackbar is an active element that can embed an action button. You can see the different displays for these two elements in Figure 1.

Fig. 1: Comparison of snackbar and toast

Fig. 1: Comparison of snackbar and toast

In both cases, the source code needed to display a snackbar or a toast is very simple. Both the snackbar and the toast have a static factory method called Make that returns a snackbar object and a toast object, respectively. As you can see in Listing 1, the Show method is called on this object in order to display the message.

Listing 1

// Create and display snackbar
var snackbar = Snackbar.Make("This is a snackbar");
await snackbar.Show();
 
// Create and display toast
var toast = Toast.Make("This is a toast");
await toast.Show();

Animations

The .NET MAUI Community Toolkit extends the already existing animations in .NET MAUI with the class FadeAnimation. A FadeAnimation sets a control’s opacity to a new value for a short time. Afterwards, the original value is automatically set again. Typically, the FadeAnimation is used to reduce the default opacity of 1 (equal to 100 percent) to a translucent value of, for example, 0.3 (equal to 30 percent) when an element is clicked. When clicked, it shows interactivity that looks like pulsing.

Behaviors

In .NET MAUI, just like WPF or Xamarin.Forms, XAML controls can be extended with additional functionality through behaviors without having to use inheritance. Behaviors provide a convenient way to encapsulate frequently used source code in a reusable way, rather than writing it into the code-behind file of an XAML page each time.

The .NET MAUI Community Toolkit comes with some commonly used behaviors that you can use directly in your apps. You can see a list of the behaviors in Table 1.

Behavior Description
AnimationBehavior Allows any animation to play when clicking on an element.
CharactersValidationBehavior Provides the possibility to display an element differently, based on input validation rules, for instance, in red font for a invalid input.
EmailValidationBehavior Behavior to check email addresses for validity.
EventToCommandBehavior EventToCommandBehavior can be used to execute commands in the associated ViewModel when any control event is triggered.
IconTintColorBehavior Allows image colorizing, for example to display icons that are only available in one color in different states (green = ok, red = error, gray = inactive ...).
MaskedBehavior Allows the configuration of an input mask for an input field in a defined format ( such as an ISBN or credit card number).
MaxLengthReachedBehavior Performs an action when the maximum input length of an input field is reached.
MultiValidationBehavior Allows defining multiple validation behaviors for the same input element.
NumericValidationBehavior Checks if an input is a number. Optionally, a value range and the maximum number of decimal places can be configured.
ProgressBarAnimationBehavior Animates a ProgressBar when changing the value.
RequiredStringValidationBehavior Checks whether an input corresponds to a predefined string.
SelectAllTextBehavior This Behavior can be used to select the complete text from an input field once it gets the focus.
SetFocusOnEntryCompletedBehavior Allows you to set the focus to the next input element if the...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK