In .NET and Xamarin.Forms, which .NET MAUI is based on, We can use the [DependsOn] attribute from the Xamarin Community Toolkit's MVVM helpers to specify that one property depends on another. Here's an example of how you can use it:
using Xamarin.CommunityToolkit.MVVM;
public class MyViewModel : BaseViewModel
{
private string firstName;
private string lastName;
private string fullName;
public string FirstName
{
get => firstName;
set => SetProperty(ref firstName, value);
}
public string LastName
{
get => lastName;
set => SetProperty(ref lastName, value);
}
[DependsOn(nameof(FirstName), nameof(LastName))]
public string FullName
{
get => $"{FirstName} {LastName}";
}
}
I'm trying to achieve the same in .Net MAUI, but not getting this attribute over there. I'm using .Net 6 as of now for .Net MAUI.
For .NET MAUI the
CommunityToolkit.Mvvm
is the one you want to migrate to (i.e. notXamarin.CommunityToolkit.Mvvm
).The changes you need to make are:
class
topartial class
ObservableObject
FirstName
andLastName
FirstName
andLastName
withObservableProperty
attributeNotifyPropertyChangedFor
FullName
property is OK, but can be shortened furtherCheck out James Montemagno's YouTube videos on the topic, such as Even More MVVM Source Generator Awesomeness for .NET Developers
[EDIT]
A .NET 6 working sample of the above can be found here: https://github.com/stephenquan/maui-dotnet-6-app
Check https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/ the documentation where it mentions that it targets both .NET Standard and special mention for the .NET 6 target: