XAML .NET MAUI命令从未执行

jv4diomz  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(259)

我在.NET MAUI中有一个视图,其中包含一些按钮,如下所示:

<Button Text="d2" Command="{Binding RollCommand}" CommandParameter="2" />
<Button Text="d4" Command="{Binding RollCommand}" CommandParameter="4" />
<Button Text="d6" Command="{Binding RollCommand}" CommandParameter="6" />
<Button Text="d8" Command="{Binding RollCommand}" CommandParameter="8" />
<Button Text="d10" Command="{Binding RollCommand}" CommandParameter="10" />
<Button Text="d12" Command="{Binding RollCommand}" CommandParameter="12" />
<Button Text="d20" Command="{Binding RollCommand}" CommandParameter="20" />

为上面的视图设置了数据上下文:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:DiceRoller2"
             x:Class="DiceRoller2.MainPage">

    <ContentPage.BindingContext>
        <local:DiceRollerViewModel />
    </ContentPage.BindingContext>

    // ...

而命令是在这个视图模型中定义的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;

namespace DiceRoller2
{
    [INotifyPropertyChanged]
    public partial class DiceRollerViewModel
    {
        public DiceRollerViewModel()
        {
            RollCommand = new Command<int>(Roll);
        }

        public int Dice { get; set; }

        public string Result { get; set; }

        public ICommand RollCommand { get; set; }

        public void Roll(int sides)
        {
            var rolls = new List<int>();
            for (var i = 0; i < Dice; i++)
            {
                rolls.Add(Random.Shared.Next(1, sides + 1));
            }
            Result = $"Rolled {Dice}d{sides} and got {string.Join(", ", rolls)} for a total of {rolls.Sum()}.";
        }
    }
}

但是,当我运行应用程序时,单击按钮什么也不做;即使我在Roll方法中设置了断点,它也不会被命中,即使视图模型的构造函数中的断点确实被命中。
我可能遗漏了什么?为什么掷骰子的命令没有执行?不幸的是,Visual Studio中的WPF数据绑定调试选项似乎不适用于MAUI...

h22fl7wq

h22fl7wq1#

首先,建议在MAUI项目中使用MVVM时使用CommunityToolkit.Mvvm
然后我认为这个问题与CommandParameter有关,它应该是String类型而不是Integer .你可以参考我下面的代码片段:

**1.**在项目中安装nuget包CommunityToolkit.Mvvm
**2.****检视:**主页面.xaml

<Button WidthRequest="100" HeightRequest="50" Text="Add" Command="{Binding RollCommand} " CommandParameter="22"></Button>

在代码隐藏中,不要忘记使用绑定ViewModel:

public MainPage()
    {
        InitializeComponent();
        BindingContext = new DiceRollerViewModel();
    }

**3.****检视模型:**骰子滚轮检视模型. cs

使用MVVM时,VM应按如下方式实现ObservableObject

using CommunityToolkit.Mvvm.ComponentModel;
using System.Diagnostics;
using System.Windows.Input;

namespace MauiApptest01
{
    public class DiceRollerViewModel : ObservableObject
    {
        public ICommand RollCommand { get; set; }

        public DiceRollerViewModel()
        {

            RollCommand = new Command<string>(Roll);
        }
        private void Roll(string sides)
        {
             Debug.WriteLine("Answer: This is a test "+ sides);
        }
    }
}

**4.**单击该按钮时,将在调试输出中触发该命令,如下所示:

Answer: This is a test22

相关问题