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

Net MAUI中[NotifyCanExecuteChangedFor]目标需为可访问IRelayCommand属性报错

问题解决:[NotifyCanExecuteChangedFor]报错“目标必须是可访问的IRelayCommand属性”

问题原因

报错的核心是**[NotifyCanExecuteChangedFor]指定的目标名称错误**。当你给带参数的方法标记[RelayCommand]特性时,MVVM Toolkit会自动生成名为[方法名]Command的IRelayCommand属性——比如你的SetBusy方法会生成SetBusyCommand,而你当前写的nameof(SetBusy)指向的是原方法,并非生成的命令属性,所以工具无法识别到合法的IRelayCommand目标。

修正后的代码

将[NotifyCanExecuteChangedFor(nameof(SetBusy))]替换为[NotifyCanExecuteChangedFor(nameof(SetBusyCommand))]即可解决问题:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; 

namespace uManageMobileApp.ViewModel
{
    public partial class BaseViewModel : ObservableObject
    {
        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(SetBusyCommand))]
        public bool isBusy = false;

        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(SetBusyCommand))]
        public string? busyLable = string.Empty;


        [RelayCommand]
        public async void Back()
        {
            await Shell.Current.GoToAsync($"..");
        }

        [RelayCommand]
        public void SetBusy(string busyText)
        {
            this.IsBusy = true;
            this.BusyLable = busyText;
        }

        [RelayCommand]
        public void SetNotBusy()
        {
            this.IsBusy = false;
            this.BusyLable = string.Empty;
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 17:20:13