You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7 MAUI中RadioButton选中时仅触发RelayCommand的问题

解决MAUI RadioButton仅在选中时触发RelayCommand的问题

方法1:使用EventArgsConverter过滤触发条件

这种方法通过转换器判断RadioButton的选中状态,仅在选中时执行命令,无需修改原有ViewModel逻辑。

步骤1:创建转换器

在项目中添加一个转换器类,用于判断CheckedChanged事件的状态:

using Microsoft.Maui.Controls;
using System.Globalization;

namespace YourNamespace.Converters
{
    public class CheckedTriggerConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 仅当RadioButton被选中时返回true,触发命令
            return value is CheckedChangedEventArgs args && args.Value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

步骤2:在XAML中引用转换器

在Settings页面的资源中声明转换器(替换YourNamespace为实际命名空间):

<ContentPage.Resources>
    <converters:CheckedTriggerConverter x:Key="CheckedTriggerConverter" />
</ContentPage.Resources>

步骤3:修改RadioButton的EventToCommandBehavior

为每个RadioButton的行为添加EventArgsConverter,指定我们创建的转换器:

<RadioButton GroupName="inventory_value" x:Name="case_each" Content="1 case, 5 each" IsChecked="True">
    <RadioButton.Behaviors>
        <toolkit:EventToCommandBehavior 
            EventName="CheckedChanged" 
            Command="{Binding UpdateSettingCommand}" 
            CommandParameter="inventory_value:case_each"
            EventArgsConverter="{StaticResource CheckedTriggerConverter}" />
    </RadioButton.Behaviors>
</RadioButton>

<RadioButton GroupName="inventory_value" x:Name="case_fraction" Content="1 1/2 cases">
    <RadioButton.Behaviors>
        <toolkit:EventToCommandBehavior 
            EventName="CheckedChanged" 
            Command="{Binding UpdateSettingCommand}" 
            CommandParameter="inventory_value:case_fraction"
            EventArgsConverter="{StaticResource CheckedTriggerConverter}" />
    </RadioButton.Behaviors>
</RadioButton>

方法2:绑定IsChecked到ViewModel属性(更贴合MVVM)

通过将RadioButton的IsChecked绑定到ViewModel的属性,在属性的setter中仅当选中状态为true时执行保存逻辑。

步骤1:在ViewModel中添加对应属性

为每个RadioButton添加布尔属性,并在setter中处理保存:

private bool _isCaseEachChecked = true;
public bool IsCaseEachChecked
{
    get => _isCaseEachChecked;
    set
    {
        if (SetProperty(ref _isCaseEachChecked, value) && value)
        {
            _ = UpdateSettingsCommand.Execute("inventory_value:case_each");
        }
    }
}

private bool _isCaseFractionChecked;
public bool IsCaseFractionChecked
{
    get => _isCaseFractionChecked;
    set
    {
        if (SetProperty(ref _isCaseFractionChecked, value) && value)
        {
            _ = UpdateSettingsCommand.Execute("inventory_value:case_fraction");
        }
    }
}

步骤2:修改XAML中的RadioButton绑定

移除EventToCommandBehavior,直接绑定IsChecked到ViewModel的属性:

<RadioButton GroupName="inventory_value" x:Name="case_each" Content="1 case, 5 each" 
             IsChecked="{Binding IsCaseEachChecked}" />

<RadioButton GroupName="inventory_value" x:Name="case_fraction" Content="1 1/2 cases" 
             IsChecked="{Binding IsCaseFractionChecked}" />

两种方法都能实现仅在RadioButton被选中时触发保存逻辑,方法1适合保留事件转命令的方式,方法2更符合MVVM的属性绑定思想。

内容的提问来源于stack exchange,提问作者jakewags01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:17:54