WPF V4 - MVVM Light V4 (BL16 - Mix11) - RelayCommand - CanExecute 不触发

发布于 2024-11-07 09:40:41 字数 1284 浏览 0 评论 0原文

我正在尝试在使用 MVVM-light V3 Sp1 的 WPF v4 应用程序中使用最新预览版的 MVVM-Light (V4 - BL16 - Mix11)。

在我的 ViewModel 中,我定义了以下属性,

    private WifiNetwork _selectedNetwork;
    public WifiNetwork SelectedNetwork 
    {
        get
        {
            return _selectedNetwork;
        }
        set
        {
            if (_selectedNetwork == value)
                return;

            _selectedNetwork = value;
            RaisePropertyChanged("SelectedNetwork");
        }
    }

此属性绑定到 View XAML 中的 ListBox 的 SelectedItem。

我在我的 viewModel 中定义了以下 RelayCommand

    private RelayCommand _connectCommand;
    public RelayCommand ConnectCommand
    {
        get
        {
            if (_connectCommand == null)
            {
                _connectCommand = new RelayCommand(
                    () => ConnectToSpecifiedNetwork(SelectedNetwork),
                    () => SelectedNetwork != null && ! SelectedNetwork.IsConnected);
            }

            return _connectCommand;
        }
    }

此 RelayCommand 绑定到我的“连接”按钮。

当所选项目尚未连接(IsConnected 为 false)时,应启用连接按钮。

这在 MVVM-Light V3 Sp1 中完美运行。但是当我使用 MIX 11 预览版的 MVVM-Light 时,这似乎不再起作用了。

有什么建议吗?

如果需要,我可以将整个项目上传到某个地方。

I am trying to use the latest Preview version of MVVM-Light (V4 - BL16 - Mix11) in my WPF v4 app that was using MVVM-light V3 Sp1.

In my ViewModel, I have the following property defined

    private WifiNetwork _selectedNetwork;
    public WifiNetwork SelectedNetwork 
    {
        get
        {
            return _selectedNetwork;
        }
        set
        {
            if (_selectedNetwork == value)
                return;

            _selectedNetwork = value;
            RaisePropertyChanged("SelectedNetwork");
        }
    }

This property is binded to ListBox's SelectedItem in the View XAML.

I have the following RelayCommand defined in my viewModel

    private RelayCommand _connectCommand;
    public RelayCommand ConnectCommand
    {
        get
        {
            if (_connectCommand == null)
            {
                _connectCommand = new RelayCommand(
                    () => ConnectToSpecifiedNetwork(SelectedNetwork),
                    () => SelectedNetwork != null && ! SelectedNetwork.IsConnected);
            }

            return _connectCommand;
        }
    }

This RelayCommand is binded to my "Connect" button.

When the selected item is not already connected (IsConnected is false), then the connect button should enable.

This worked perfect in MVVM-Light V3 Sp1. But when I use the MIX 11 preview version of MVVM-Light, this doesn't seem to work anymore.

Any suggestions?

I can upload my entire project somewhere if needed..

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

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

发布评论

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

评论(2

墨落画卷 2024-11-14 09:40:41

原因是删除了对 CommandManager 的调用。如果您将其添加回来,则无需手动引发它。但这在 silverlight 中不起作用。以下是似乎已删除的 V3 代码:

    /// <summary>
    /// Occurs when changes occur that affect whether the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (_canExecute != null)
            {
                CommandManager.RequerySuggested += value;
            }
        }

        remove
        {
            if (_canExecute != null)
            {
                CommandManager.RequerySuggested -= value;
            }
        }
    }

    public void RaiseCanExecuteChanged()
    {
#if SILVERLIGHT
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
#else
        CommandManager.InvalidateRequerySuggested();
#endif
    }

链接到 V3 RelayCommand

链接到 V4 RelayCommand

The reason is that the call to the CommandManager was removed. If you add this back, you won't have to manually raise it. This won't work in silverlight though. Here is the code from V3 that appears to be removed:

    /// <summary>
    /// Occurs when changes occur that affect whether the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (_canExecute != null)
            {
                CommandManager.RequerySuggested += value;
            }
        }

        remove
        {
            if (_canExecute != null)
            {
                CommandManager.RequerySuggested -= value;
            }
        }
    }

    public void RaiseCanExecuteChanged()
    {
#if SILVERLIGHT
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
#else
        CommandManager.InvalidateRequerySuggested();
#endif
    }

Link to V3 RelayCommand

Link to V4 RelayCommand

你爱我像她 2024-11-14 09:40:41

为了解决这个问题,我必须在 INPC 属性上调用“RaiseCanExecuteChanged()”方法,如下所示,但

    private WifiNetwork _selectedNetwork;
    public WifiNetwork SelectedNetwork 
    {
        get
        {
            return _selectedNetwork;
        }
        set
        {
            if (_selectedNetwork == value)
                return;

            _selectedNetwork = value;

            RaisePropertyChanged("SelectedNetwork");
            // FIX - raise the CanExecuteChanged on the command.
           _connectCommand.RaiseCanExecuteChanged();
        }
    }

在 MVVM-Light V3 Sp1 中我不必执行此操作(在命令上手动引发 RaiseCanExecuteChanged)。

In order to fix this issue, I had to call "RaiseCanExecuteChanged()" method on my INPC property as shown below

    private WifiNetwork _selectedNetwork;
    public WifiNetwork SelectedNetwork 
    {
        get
        {
            return _selectedNetwork;
        }
        set
        {
            if (_selectedNetwork == value)
                return;

            _selectedNetwork = value;

            RaisePropertyChanged("SelectedNetwork");
            // FIX - raise the CanExecuteChanged on the command.
           _connectCommand.RaiseCanExecuteChanged();
        }
    }

I didn't have to do this (manually raise the RaiseCanExecuteChanged on a command) in MVVM-Light V3 Sp1.

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