Search code examples
c#xamlmauicommunity-toolkit-mvvm

How can I fix this CommunityToolkit.MVVM error message when I install it in my maui application?


w When I go to the NuGet Package Manager in VS 2022 and download CommunityToolkit.MVVM, it gives me the followin error message:

This version of the MVVM Toolkit requires 'Microsoft.Windows.SDK.NET.Ref' version '10.0.19041.38' or later. Please update to .NET SDK 8.0.109, 8.0.305 or 8.Comm0.402 (or later). Alternatively, use a temporary 'Microsoft.Windows.SDK.NET.Ref' reference, which can be done by setting the 'WindowsSdkPackageVersion' property in your .csproj file. For your project configuration, it is recommended to set the package version to '10.0.19041.41'.

And it's rather difficult to follow how to fix. It seems without this package, I cannot add a public method to my view model file and give it a [RelayCommand] line just above it so I can use the Command={Binding GenFileNameCommand} in my view code.

I'm using Visual Studios 2022 ver. 17.11.3 and my maui application is using a .NET 8.0, and my view model starts with: public partial class MainViewModel : INotifyPropertyChanged.

So is there a simple and easy way to fix this error, or is it just a bug on Microsoft's end?


Solution

  • See if it helps to add the WindowsSdkPackageVersion tag in your .csproj file as shown.


    You may need to reload the project after doing this.


    <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
            <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
            <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
            <WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>
            .
            .
            .
    </PropertyGroup>