简体   繁体   English

在具有CanExecute委托的MVVM Light中使用RelayCommand导致我的应用程序无法启动

[英]Using RelayCommand in MVVM Light with a CanExecute delegate causes my application not to launch

I'm having trouble getting the RelayCommand from MVVM Light to work in a WPF application, specifically when I try to use a CanExecute delegate. 我在使MVVM Light中的RelayCommand在WPF应用程序中工作时遇到困难,特别是当我尝试使用CanExecute委托时。 If I add this second parameter to the constructor of the command, the MainWindow never launches and I have to kill the application from Task Manager. 如果我将第二个参数添加到命令的构造函数中,则MainWindow将永远不会启动,并且必须从任务管理器中终止该应用程序。 This problem goes away if I remove the second parameter. 如果删除第二个参数,此问题将消失。 I've searched around and found out that MVVM Light switched to a portable class library which caused some issues with the CanExecute delegate. 我四处搜索,发现MVVM Light切换到可移植类库,这导致CanExecute委托出现了一些问题。 I've tried going back to version 4.2.32.7 of MVVM Light, and also tried using the Galasoft.MvvmLight.CommandWpf namespace, but neither of these helped. 我尝试过回溯到MVVM Light的4.2.32.7版本,还尝试使用Galasoft.MvvmLight.CommandWpf命名空间,但是这些都不起作用。

I should also mention that no build errors (it says the build succeeds) or compilation errors occur. 我还应该提到,不会发生任何构建错误(它表示构建成功)或编译错误。 Here is some simple code where I found the problem: 这是一些我发现问题的简单代码:

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
    }

    private string myString;
    public string MyString
    {
        get { return myString; }
        set { Set("MyString", ref myString, value); }
    }

    private string myOtherString;
    public string MyOtherString
    {
        get { return myOtherString; }
        set { Set("MyOtherString", ref myOtherString, value); }
    }

    private RelayCommand myCommand;
    public RelayCommand MyCommand
    {
        get
        {
            return myCommand ??
                (myCommand = new RelayCommand(
                    () => MyOtherString = MyString,
                    () => MyString.Length > 5));
        }
    }
}

My view is just two text boxes: one bound to MyString and one bound to MyOtherString. 我的视图只有两个文本框:一个绑定到MyString,一个绑定到MyOtherString。 There is also a button bound to MyCommand. 还有一个绑定到MyCommand的按钮。 Does anyone know why using the CanExecute parameter breaks my application? 有谁知道为什么使用CanExecute参数会破坏我的应用程序?

The MyString property in your viewmodel is null during launch and throwing null reference. 您的视图模型中的MyString属性在启动和引发空引用期间为null。 So either do null check on the CanExecute or assign default value to MyString property. 因此,要么对CanExecute进行null检查,要么将默认值分配给MyString属性。 Refer below code. 请参考下面的代码。

 public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
    }

    private string myString = string.Empty;
    public string MyString
    {
        get { return myString; }
        set { Set("MyString", ref myString, value); }
    }

    private string myOtherString;
    public string MyOtherString
    {
        get { return myOtherString; }
        set { Set("MyOtherString", ref myOtherString, value); }
    }

    private RelayCommand myCommand;
    public RelayCommand MyCommand
    {
        get
        {
            return myCommand ??
                (myCommand = new RelayCommand(
                    () => MyOtherString = MyString,
                    () => MyString?.Length > 5));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM