简体   繁体   English

无法在使用 CommunityToolkit.Mvvm 的视图模型中使用 ICommand 属性

[英]Can't use ICommand attribute in view model using CommunityToolkit.Mvvm

In my view models, I wanted to use the source generators in CommunityToolkit.Mvvm but for some reason I can't seem to use [ICommand] attribute with my action methods.在我的视图模型中,我想在 CommunityToolkit.Mvvm 中使用源生成器,但由于某种原因,我似乎无法将[ICommand]属性与我的操作方法一起使用。

The error I get is:我得到的错误是:

Cannot apply attribute class 'ICommand' because it is abstract无法应用属性类“ICommand”,因为它是抽象的

Here's the base class for my view model model.这是我的视图模型模型的基类。

using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp.ViewModels
{
    public partial class BaseViewModel : ObservableObject
    {
        [ObservableProperty]
        bool isBusy = false;

        [ObservableProperty]
        string title = string.Empty;
    }
}

And here's my view model class:这是我的视图模型类:

public class MyViewModel : BaseViewModel
{
   [ObservableProperty]
   string firstName;

   [ObservableProperty]
   string lastName;

   [ICommand] // <-- This is where I get the error I mentioned above
   async Task DoSomething()
   {
       // Do something here...
   }
}

The problem seems to be what @Luca Clavarino mentioned in the comments:问题似乎是@Luca Clavarino 在评论中提到的:

Perhaps you're accidentally using the ICommand interface from System.Windows.Input,instead of the ICommandAttribute from the CommunityTookit.也许您不小心使用了 System.Windows.Input 中的 ICommand 接口,而不是 CommunityTookit 中的ICommandAttribute Try to replace [ICommand] with [CommunityToolkit.Mvvm.Input.ICommand] and see if that was the case.尝试将[ICommand]替换为[CommunityToolkit.Mvvm.Input.ICommand]并查看是否是这种情况。

And I think I know why this might be happening to you.我想我知道为什么这会发生在你身上。 The ICommandAttribute seems to be missing in CommunityToolkit.Mvvm 8.0.0-preview4 so intellisense won't offer the using CommunityToolkit.Mvvm.Input statement and instead offers using System.Windows.Input; CommunityToolkit.Mvvm 8.0.0-preview4 中似乎缺少ICommandAttribute因此智能感知不会提供using CommunityToolkit.Mvvm.Input语句,而是提供using System.Windows.Input; . .

The problem can be resolved by downgrading to CommunityToolkit.Mvvm 8.0.0-preview3 , that version works fine for me.该问题可以通过降级到 CommunityToolkit.Mvvm 8.0.0-preview3来解决,该版本对我来说很好。

Here's a working sample (using CommunityToolkit.Mvvm 8.0.0-preview3).这是一个工作示例(使用 CommunityToolkit.Mvvm 8.0.0-preview3)。

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace MyApp.ViewModels
{
    public partial class BaseViewModel : ObservableObject
    {
        [ObservableProperty]
        bool isBusy = false;

        [ObservableProperty]
        string title = string.Empty;
    }

    public partial class MyViewModel : BaseViewModel
    {
        [ObservableProperty]
        string firstName;

        [ObservableProperty]
        string lastName;

        [ICommand] //works in 8.0.0-preview3
        async Task DoSomething()
        {
            // Do something here...
        }
    }
}

I've also noticed that while the ICommandAttribute is gone in 8.0.0-preview4, there's a RelayCommandAttribute instead .我还注意到,虽然ICommandAttribute在 8.0.0-preview4 中消失了,但有一个RelayCommandAttribute代替 Maybe they've simply renamed it.也许他们只是简单地重命名了它。

Using the RelayCommandAttribute instead of ICommandAttribute in 8.0.0-preview4 seems to be working.在 8.0.0-preview4 中使用RelayCommandAttribute而不是ICommandAttribute似乎有效。

[RelayCommand] //works in 8.0.0-preview4
async Task DoSomething()
{
    // Do something here...
}

EDIT编辑

Yes, they've renamed the ICommandAttribute to RelayCommandAttribute .是的,他们已将ICommandAttribute重命名为RelayCommandAttribute It's metioned in the breaking changes section of the 8.0.0-preview4 release notes .它在8.0.0-preview4 发行说明的重大更改部分中提到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 CommunityToolkit.Mvvm 调用事件 - How to call events using CommunityToolkit.Mvvm 使用 CommunityToolkit.Mvvm 通知 CanExecuteChanged 的 RelayCommand 时出现 StackOverflow 异常 - StackOverflow Exception when notifying RelayCommand of CanExecuteChanged using CommunityToolkit.Mvvm 使用 CommunityToolkit.Mvvm 在 ObservableProperty 更改时调用方法 - Call method when ObservableProperty changes using CommunityToolkit.Mvvm C# CommunityToolkit.Mvvm ObservableProperty 列表 - C# CommunityToolkit.Mvvm ObservableProperty on a list 使用 CommunityToolkit.Mvvm 处理可观察对象属性 - Handling observable object properties with CommunityToolkit.Mvvm 如何将 CommunityToolkit.Mvvm 中的源代码生成器用于 .NET 框架 4.7.2 WPF 应用程序 - How to use the source generators from CommunityToolkit.Mvvm for a .NET Framework 4.7.2 WPF Application 如何在 C# 中使用 Community - How do I bind an object that is periodically modified to a treeview in C# WPF using the CommunityToolkit.MVVM? 显示 ObservableGroupedCollection 的正确方法<string, telement>使用 Wpf .NET 6 和 CommunityToolkit.Mvvm 包</string,> - Proper way of displaying an ObservableGroupedCollection<string, TElement> using Wpf .NET 6 and the CommunityToolkit.Mvvm Package 使用 CommunityToolkit.Mvvm 和依赖注入更新视图模型的属性 - Update Properties across Viewmodels with CommunityToolkit.Mvvm and Dependency Injection CommunityToolkit.MVVM 的依赖注入如何工作? - How does the dependency injection with the CommunityToolkit.MVVM work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM