如何使用MVVM RelayCommand属性?

huangapple go评论64阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月7日 05:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定