I am following the excellent Learn .NET MAUI - Full Course for Beginners from James Montemagno https://github.com/dotnet-presentations/dotnet-maui-workshop?WT.mc_id=dotnet-29192-cxa
In this tutorial he explains how to use the CommunityToolKit.Mvvm I have some problems using it (I am using V8.4.0) For a basic usage you declare something like this:
using CommunityToolkit.Mvvm.ComponentModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace DreamMachineApp.ViewModel
{
public partial class BaseViewModel : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsNotBusy))]
bool isBusy;
[ObservableProperty]
private string? title;
bool IsNotBusy => !IsBusy;
}
}
The first problem is that I get the following warning for the declaration of isBusy and title: MVVM Toolkit warning MVVMTK0045 : The field DreamMachineApp.ViewModel.BaseViewModel.isBusy using [ObservableProperty] will generate code that is not AOT compatible in WinRT scenarios (such as UWP XAML and WinUI 3 apps), and a partial property should be used instead (as it allows the CsWinRT generators to correctly produce the necessary WinRT marshalling code)
It is only a warning but I would like to get rid of it. I have tried what is advice here but its even worse. What puzzle me is in the project provided by James (using the exact same code) I do not see the error? May be some settings to be done?
The second problem is that if I look in the dependencies I see tons of warning in the toolkit
In fact you can see that in ObservablePropertyGenerator has been working successfully and the application works OK but This looks scary! I am on the lates version of Visual Studio using .net9.0 (very new release – may be the problem?)
I have tried what is recommended here: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/errors/mvvmtk0045 but it does not help
I would appreciate if someone has a solution and can explain why on the project from James there is no error ?
I created a new .net 9.0 maui project and reproduced your problem with the CommunityToolKit.Mvvm version 8.4.0. And you can get rid of the warning by the following two ways:
You can add the following code into the csproj file:
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>
And then change your code to:
public partial class BaseViewModel : ObservableObject
{
[ObservableProperty]
public partial bool Isbusy { get; set; }
[ObservableProperty]
public partial string? Title { get; set; }
}