MVVM - stephenquan/SQuan.Helpers.Maui GitHub Wiki
Introduction
The SQuan.Helpers.Maui.Mvvm
package is designed to supplement the CommunityToolkit.Mvvm package with .NET MAUI-specific source-generators to simplify creating observable properties or bindable properties on .NET MAUI bindable objects.
Getting started
To install the package from within Visual Studio:
- In Solution Explorer, right-click on the project and select "Manage NuGet Packages". Search for "SQuan.Helpers.Maui.Mvvm" and install it.
- Add a using or Imports directive to use the new APIs:
using SQuan.Helpers.Maui.Mvvm;
- Set the language version in your project file to access preview features:
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>
Examples
CardView Example
You can use [BindableProperty]
to reduce the code needed to extend ContentView
to create a custom control. The following example shows the CardTitle
bindable property in the code-behind file to the CardView
class:
public partial class CardView : ContentView
{
[BindableProperty] public partial string CardTitle { get; set; } = string.Empty;
// ...
public CardView()
{
InitializeComponent();
}
}
Count Example
You can use [ObservableProperty]
to reduce the code needed to add properties to a ContentPage
. The following example turns Count
into an observable property in the code-behind file of the MainPage
class:
public partial class MainPage : ContentPage
{
[ObservableProperty] public partial int Count { get; set; } = 0;
public MainPage()
{
InitializeComponent();
CounterBtn.SetBinding(
Button.TextProperty,
static (MainPage ctx) => ctx.Count,
BindingMode.OneWay,
source: this,
stringFormat: "Clicked {0} times");
}
void OnCounterClicked(object sender, EventArgs e)
{
Count++;
SemanticScreenReader.Announce(CounterBtn.Text);
}
}
Attributes
BindableProperty
Adds bindable properties to a bindable object.
[BindableProperty] public partial string CardTitle { get; set; } = string.Empty;
ObservableProperty
Adds observable properties to a bindable object with OnPropertyChanged() notifications.
[ObservableProperty] public partial int Count { get; set; } = 0;
NotifyPropertyChangedFor
Makes additional calls to a bindable object's OnPropertyChanged() method.
public partial class MainPage
{
[ObservableProperty, NotifyPropertyChangedFor("FullName")] public partial string FirstName { get; set; } = string.Empty;
[ObservableProperty, NotifyPropertyChangedFor("FullName")] public partial string LastName { get; set; } = string.Empty;
public string FullName => $"{FirstName} {LastName}";
}
NotifyPropertyChangingFor
Makes additional calls to a bindable object's OnPropertyChanging() method.
Events
For generated observable properties and bindable properties, additional partial methods may be implemented.
partial void On{PropertyName}Changing({PropertyType} value);
partial void On{PropertyName}Changing({PropertyType} oldValue, {PropertyType} newValue);
partial void On{PropertyName}Changed({PropertyType} value);
partial void On{PropertyName}Changed({PropertyType} oldValue, {PropertyType} newValue);
For example:
public partial class MainPage
{
[ObservableProperty] public int Count { get; internal set; } = 0;
public void OnCountChanged(int oldValue, int newValue)
{
Trace.WriteLine($"Count changed from {oldValue} to {newValue}");
}
}