英文:
How to use the MVVM RelayCommand Attribute?
问题
我尝试使用属性,并在尝试使用Community Toolkit中的RelayCommand属性时遇到了问题。
我查看了 .NET上的RelayCommand属性文档,并尝试实现一个简单的示例,如下所示:
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace HelloWorld_WinUI3.ViewModels;
public partial class MainViewModel : ObservableObject
{
public MainViewModel()
{
}
private int _counter = 0;
[RelayCommand]
private async Task DoSomethingAsync()
{
await Task.Delay(1000);
MyString = $"Hello {_counter++}";
}
[ObservableProperty]
private string _myString = "Hello";
}
这会产生错误:CS0616 'RelayCommand'不是一个属性类
有人可以解释一下这段代码有什么问题,以及如何正确使用[RelayCommand]属性吗?
英文:
I have been trying out attributes and ran in an issue trying to use the RelayCommand attribute from the Community Toolkit.
I looked over the
.NET Documentation on RelayCommand Attribute and tried to implement a simple example as follows:
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace HelloWorld_WinUI3.ViewModels;
public partial class MainViewModel : ObservableObject
{
public MainViewModel()
{
}
private int _counter = 0;
[RelayCommand]
private async Task DoSomethingAsync()
{
await Task.Delay(1000);
MyString = $"Hello {_counter++}";
}
[ObservableProperty]
private string _myString = "Hello";
}
This produces the error: CS0616 'RelayCommand' is not an attribute class
Can someone explain what is wrong with this code and how to use the [RelayCommand] attribute properly?
答案1
得分: 2
The CommunityToolkit.Mvvm 适用于 WinUI 3 和 .NET 7。实际上,我经常使用它,包括我为 StackOverflow 回答创建的每个示例。
我猜你可能遇到了这个已知的问题(https://github.com/dotnet/roslyn/issues/67123#issuecomment-1489157031)。
所以,在此时,请尝试使用 v8.0 而不是 v8.1。
英文:
The CommunityToolkit.Mvvm works with WinUI 3 and.NET7. Actually, I use it a lot, including every sample I create for StackOverflow answers.
I guess, you might be facing this known issue.
So, at this moment, try the v8.0 instead of v8.1.
答案2
得分: 1
使用MVVM源生成器在WinUI 3中仍然存在问题;我建议使用原始的Relay命令声明:
public class MyViewModel : ObservableObject
{
public MyViewModel()
{
IncrementCounterCommand = new RelayCommand(IncrementCounter);
}
private int counter;
public int Counter
{
get => counter;
private set => SetProperty(ref counter, value);
}
public ICommand IncrementCounterCommand { get; }
private void IncrementCounter() => Counter++;
}
希望这有所帮助。
英文:
Using MVVM Source generators is still problematic for WinUI 3; I suggest using raw Relay command declaration:
public class MyViewModel : ObservableObject
{
public MyViewModel()
{
IncrementCounterCommand = new RelayCommand(IncrementCounter);
}
private int counter;
public int Counter
{
get => counter;
private set => SetProperty(ref counter, value);
}
public ICommand IncrementCounterCommand { get; }
private void IncrementCounter() => Counter++;
}
答案3
得分: 0
- [ObservableProperty] 在 WinUI3 项目中似乎有效
- [RelayCommand] 在 WinUI3 项目中不起作用。
英文:
It appears the CommunityToolkit does not fully support WinUI3 or .NET 7 at this time Docs.
Running the same example on a WPF project works fine.
- [ObservableProperty] seems to work in a WinUI3 project
- [RelayCommand] does not work with WinUI3 project.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论