排除CommunityToolkit.Mvvm 8.0编译错误的方法

创建WPF工程

选择WPF Application模板,点击Next,并输入工程名称MvvmDemo,点击Next选择Framework.Net 6.0 (Long-term support),点击Create按钮完成示例工程创建。

选择工程模板

输入工程名称

选择.net版本

添加CommunityToolkit.Mvvm引用

在工程名称上右键点击后,选择Manage NuGet Packages...,并在Browse中输入CommunityToolkit.Mvvm 并选中对应的NuGet包点击 ,在弹出的窗口上点击OK按钮后同意授权协议后完成组件安装。

CommunityToolkit.Mvvm的授权协议为MIT,该协议对你的软件发行无任何限制,可以放心使用此组件。

添加引用CommunityToolkit.Mvvm


选择CommunityToolkit.Mvvm并点击安装按钮

确认安装

输入示例代码

MainWindow.xaml.cs中输入如下代码:

    public class User : ObservableObject
    {
        private string? name;
        public string? Name
        {
            get => name;
            set => SetProperty(ref name, value);
        }
    }

    public class ObservableUser : ObservableObject
    {
        private readonly User user;
        public ObservableUser(User user)
        {
            this.user = user;
        }

        public string? Name
        {
            get => user.Name;
            set => SetProperty(user.Name, value, user, (u, n) => u.Name = n);
        }
    }

    public class LoggedInUserRequestMessage
    {

    }

    public class MyViewModel : ObservableRecipient, IRecipient
    {
        public void Receive(LoggedInUserRequestMessage message)
        {
            throw new NotImplementedException();
        }
    }

编译程序

F7快捷键编译工程,此时编译输出显示错误如下:

 Build started...
1>------ Build started: Project: MvvmDemo, Configuration: Debug Any CPU ------
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\__IMessengerExtensions.g.cs(5,6,5,51): error CS0579: Duplicate 'global::System.CodeDom.Compiler.GeneratedCode' attribute
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\__IMessengerExtensions.g.cs(6,6,6,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\__IMessengerExtensions.g.cs(7,6,7,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\__IMessengerExtensions.g.cs(8,6,8,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\__IMessengerExtensions.g.cs(9,6,9,29): error CS0579: Duplicate 'global::System.Obsolete' attribute
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\__IMessengerExtensions.g.cs(10,6,10,81): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute' attribute
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\MvvmDemo.MyViewModel.g.cs(9,105,9,133): error CS0111: Type '__IMessengerExtensions' already defines a member called 'CreateAllMessagesRegistrator' with the same parameter types
1>D:\AA\MvvmDemo\MvvmDemo\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.IMessengerRegisterAllGenerator\MvvmDemo.MyViewModel.g.cs(22,113,22,150): error CS0111: Type '__IMessengerExtensions' already defines a member called 'CreateAllMessagesRegistratorWithToken' with the same parameter types
1>Done building project "MvvmDemo_qjmxbo25_wpftmp.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


编译错误

修复方法

双击Solution Explorer中的MvvmDemo ,在打开的MvvmDemo.csproj文件中增加如下代码:

    
        
            
        

        
            
            
        
    

编译修复

重新编译

在工程中输入修复代码并保存后,再次按下F7快捷键,编译通过,至此问题修复。

错误排除

参阅资料

Fix build when using source generators (#6534) by singhashish-wpf · Pull Request #6680 · dotnet/wpf · GitHub

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章