WPF 命令类重写 CanExecuteChanged-Event?

发布于 2024-11-08 20:12:19 字数 824 浏览 0 评论 0原文

目前我有以下 Command 类:

Public Class SubscribeCommand
    Implements ICommand

    Private ReadOnly _vm As MainWindowViewModel

    Public Sub New(ByVal vm As MainWindowViewModel)
        _vm = vm
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return Not String.IsNullOrEmpty(_vm.Symbol)
    End Function

    Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _vm.Subscribe()
    End Sub
End Class

在我读过的教程中,您必须为 canExecuteChanged-Event 实现 add {} 和 remove {} 。但是我怎样才能用vb.net做到这一点呢?

多谢..

At the moment I have the following Command class:

Public Class SubscribeCommand
    Implements ICommand

    Private ReadOnly _vm As MainWindowViewModel

    Public Sub New(ByVal vm As MainWindowViewModel)
        _vm = vm
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return Not String.IsNullOrEmpty(_vm.Symbol)
    End Function

    Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _vm.Subscribe()
    End Sub
End Class

In a tutorial i read, you have to implement add {} and remove {} for the canExecuteChanged-Event. But how can i do that, with vb.net?

thanks a lot..

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

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

发布评论

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

评论(2

何其悲哀 2024-11-15 20:12:19

我认为您不需要为 CanExecuteChanged 事件实现添加和删除部分。我很确定它会按照您现在的方式正常工作。但是,如果您出于某种原因确实想要(使其与您在 C# 中的这篇文章, 例如),您将更

Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs)

改为

Public Custom Event CanExecuteChanged As EventHandler

    AddHandler(ByVal value As EventHandler)

        CommandManager.RequestSuggested += value

    End AddHandler

    RemoveHandler(ByVal value As EventHandler)

           CommandManager.RequestSuggested -= value

    End RemoveHandler

End Event

I don't think you're required to implement the add and remove pieces for the CanExecuteChanged event. I'm pretty sure it'll work just fine the way you have it now. But if you did want to for some reason (to make it match what you see in this post in C#, for instance), you would change

Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs)

to

Public Custom Event CanExecuteChanged As EventHandler

    AddHandler(ByVal value As EventHandler)

        CommandManager.RequestSuggested += value

    End AddHandler

    RemoveHandler(ByVal value As EventHandler)

           CommandManager.RequestSuggested -= value

    End RemoveHandler

End Event
沉溺在你眼里的海 2024-11-15 20:12:19

如果你想在 wpf 中使用命令,请查看那里的 wpf 框架。你会发现至少有两个不错的命令实现:

if you wanna use Commands in wpf please look at the wpf frameworks out there. you will find at least two nice command implementation:

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