[英]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.