将 Command CanExecute 和 Executed 处理程序置于主窗口类之外的问题

发布于 2024-12-14 23:58:46 字数 1475 浏览 2 评论 0原文

基本上,我已经将命令本身的命令绑定分配给了 Window.CommandBindings:

<CommandBinding Command="local:TimerViewModel.AddTimer" 
    CanExecute="local:TimerViewModel.AddTimer_CanExecute" 
    Executed="local:TimerViewModel.AddTimer_Executed" />

local 是默认生成的命名空间,指向应用程序的命名空间。我在这里试图实现的是在 TimerViewModel 内部进行命令处理,但我不断收到以下错误:

CanExecute="local:TimerViewModel.AddTimer_CanExecute" 无效。 “local:TimerViewModel.AddTimer_CanExecute”不是有效的事件处理程序方法名称。只有生成的类或代码隐藏类上的实例方法才有效。

TimerViewModel 虽然非常简单,但我相信我遗漏了一些东西:

public class TimerViewModel : ViewModelBase
{
    public TimerViewModel()
    {
        _timers = new ObservableCollection<TimerModel>();
        _addTimer = new RoutedUICommand("Add Timer", "AddTimer", GetType());
    }

    private ObservableCollection<TimerModel> _timers;

    public ObservableCollection<TimerModel> Timers
    {
        get { return _timers; }
    }

    private static RoutedUICommand _addTimer;

    public static RoutedUICommand AddTimer
    {
        get { return _addTimer; }
    }

    public void AddTimer_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    public void AddTimer_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        _timers.Add(new TimerModel(TimeSpan.FromSeconds((new Random()).Next())));
    }
}

任何人都可以指出我所犯的错误吗?

Basically I've got a command binding for the command itself assigned to Window.CommandBindings:

<CommandBinding Command="local:TimerViewModel.AddTimer" 
    CanExecute="local:TimerViewModel.AddTimer_CanExecute" 
    Executed="local:TimerViewModel.AddTimer_Executed" />

local is a namespace generated by default pointing to the namespace of the application. What I'm trying to achieve here is to have the command handling inside the TimerViewModel but I keep getting the following error:

CanExecute="local:TimerViewModel.AddTimer_CanExecute" is not valid. 'local:TimerViewModel.AddTimer_CanExecute' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid.

The TimerViewModel is pretty simple though but I believe I am missing something:

public class TimerViewModel : ViewModelBase
{
    public TimerViewModel()
    {
        _timers = new ObservableCollection<TimerModel>();
        _addTimer = new RoutedUICommand("Add Timer", "AddTimer", GetType());
    }

    private ObservableCollection<TimerModel> _timers;

    public ObservableCollection<TimerModel> Timers
    {
        get { return _timers; }
    }

    private static RoutedUICommand _addTimer;

    public static RoutedUICommand AddTimer
    {
        get { return _addTimer; }
    }

    public void AddTimer_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    public void AddTimer_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        _timers.Add(new TimerModel(TimeSpan.FromSeconds((new Random()).Next())));
    }
}

Can anyone point out the mistakes I'm making?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情别恋 2024-12-21 23:58:46

另请参阅 Josh Smith 的 RelayCommand。使用它将使您能够像这样编写上面的内容:

public class TimerViewModel : ViewModelBase {
    public TimerViewModel() {
        Timers = new ObservableCollection<TimerModel>();
        AddTimerCommand = new RelayCommand(() => AddTimer());
    }

    public ObservableCollection<TimerModel> Timers {
        get;
        private set;
    }

    public ICommand AddTimerCommand {
        get;
        private set;
    }

    private void AddTimer() {
        Timers.Add(new TimerModel(TimeSpan.FromSeconds((new Random()).Next())));
    }
}

Also take a look at Josh Smith's RelayCommand. Using it would enable you to write the above like this:

public class TimerViewModel : ViewModelBase {
    public TimerViewModel() {
        Timers = new ObservableCollection<TimerModel>();
        AddTimerCommand = new RelayCommand(() => AddTimer());
    }

    public ObservableCollection<TimerModel> Timers {
        get;
        private set;
    }

    public ICommand AddTimerCommand {
        get;
        private set;
    }

    private void AddTimer() {
        Timers.Add(new TimerModel(TimeSpan.FromSeconds((new Random()).Next())));
    }
}
痕至 2024-12-21 23:58:46

查看 http://www.wpftutorial.net/DelegateCommand.html 的示例如何实现 WPF 的委托命令。它允许您将 Execute 和 CanExecute 连接为事件处理程序。如果您直接使用 RoutedUICommand,则需要从中派生自定义命令并使用您的函数覆盖 Execute 和 CanExecute。

Take a look at http://www.wpftutorial.net/DelegateCommand.html for an example of how to implement the delegate command for WPF. It allows you to hook up Execute and CanExecute as event handlers. If you're using RoutedUICommand directly you need to derive a custom command from it and override Execute and CanExecute with your functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文