简体   繁体   中英

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. 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. 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. 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.

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. There is also a button bound to MyCommand. Does anyone know why using the CanExecute parameter breaks my application?

The MyString property in your viewmodel is null during launch and throwing null reference. So either do null check on the CanExecute or assign default value to MyString property. 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));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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