Wpf 无误地断开绑定

Wpf 无误地断开绑定,wpf,mvvm,binding,Wpf,Mvvm,Binding,我有一个带有两个窗口的简单WPF应用程序,我想将其更改为使用MVVM模型 我开始用一个按钮和一个messagebox创建一个简单的绑定,但它不起作用。我的命令的Execute函数从未被调用,但我在输出窗口中没有得到绑定错误 我使用以下方法调试和跟踪绑定: 跟踪如下所示: System.Windows.Data Warning: 54 : Created BindingExpression (hash=8493835) for Binding (hash=45202970) System.Win

我有一个带有两个窗口的简单WPF应用程序,我想将其更改为使用MVVM模型

我开始用一个按钮和一个messagebox创建一个简单的绑定,但它不起作用。我的命令的Execute函数从未被调用,但我在输出窗口中没有得到绑定错误

我使用以下方法调试和跟踪绑定:

跟踪如下所示:

System.Windows.Data Warning: 54 : Created BindingExpression (hash=8493835) for Binding (hash=45202970)
System.Windows.Data Warning: 56 :   Path: 'LoginCommand'
System.Windows.Data Warning: 58 : BindingExpression (hash=8493835): Default mode resolved to OneWay
System.Windows.Data Warning: 59 : BindingExpression (hash=8493835): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 60 : BindingExpression (hash=8493835): Attach to System.Windows.Controls.Button.Command (hash=17604022)
System.Windows.Data Warning: 65 : BindingExpression (hash=8493835): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=8493835): Found data context element: Button (hash=17604022) (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=8493835): Activate with root item LoginViewModel (hash=47369058)
System.Windows.Data Warning: 106 : BindingExpression (hash=8493835):   At level 0 - for LoginViewModel.LoginCommand found accessor ReflectPropertyDescriptor(LoginCommand)
System.Windows.Data Warning: 102 : BindingExpression (hash=8493835): Replace item at level 0 with LoginViewModel (hash=47369058), using accessor ReflectPropertyDescriptor(LoginCommand)
System.Windows.Data Warning: 99 : BindingExpression (hash=8493835): GetValue at level 0 from LoginViewModel (hash=47369058) using ReflectPropertyDescriptor(LoginCommand): RelayCommand (hash=32714449)
System.Windows.Data Warning: 78 : BindingExpression (hash=8493835): TransferValue - got raw value RelayCommand (hash=32714449)
System.Windows.Data Warning: 82 : BindingExpression (hash=8493835): TransferValue - implicit converter produced <null>
System.Windows.Data Warning: 87 : BindingExpression (hash=8493835): TransferValue - using final value <null>
RelayCommand.cs:

class RelayCommand
{
    private Action<object> _execute;

    public RelayCommand(Action<object> execute)
    {
        _execute = execute;
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        // breakpoint; code execution never reaches here
        _execute(parameter);
    }

    #endregion
}
类中继命令
{
私人行动——执行;
公共中继命令(操作执行)
{
_执行=执行;
}
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
//断点;代码执行永远不会到达此处
_执行(参数);
}
#端区
}

您的
RelayCommand
是一个普通类,它没有实现
ICommand
。这将有助于:

class RelayCommand : ICommand
{
    private Action<object> _execute;

    public RelayCommand(Action<object> execute)
    {
        _execute = execute;
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        // breakpoint; code execution never reaches here
        _execute(parameter);
    }

    #endregion
}
类RelayCommand:ICommand
{
私人行动——执行;
公共中继命令(操作执行)
{
_执行=执行;
}
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
//断点;代码执行永远不会到达此处
_执行(参数);
}
#端区
}

我假设您的所有类在代码中都是“公共的”?不。。。它们具有默认的可见性。我在另一个项目中尝试过这个教程(),它很有效。这是实际的代码吗?它不会编译,这一行是错误的:MessageBox.Show(obj.ToString());是的,这就是问题所在!谢谢,这样一个蹩脚的错误!但是我仍然没有得到跟踪消息。。。TransferValue—隐式转换器的产生是如此的毫无意义。
class LoginViewModel
{
    private RelayCommand m_LoginCommand;
    public RelayCommand LoginCommand
    {
        get
        {
            if (m_LoginCommand == null) 
            {
                m_LoginCommand = new RelayCommand(param => this.Login(param));
            }
            return m_LoginCommand;
        }
    }

    public void Login(object obj)
    {
        MessageBox.Show(obj.ToString());
    }
}
class RelayCommand
{
    private Action<object> _execute;

    public RelayCommand(Action<object> execute)
    {
        _execute = execute;
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        // breakpoint; code execution never reaches here
        _execute(parameter);
    }

    #endregion
}
class RelayCommand : ICommand
{
    private Action<object> _execute;

    public RelayCommand(Action<object> execute)
    {
        _execute = execute;
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        // breakpoint; code execution never reaches here
        _execute(parameter);
    }

    #endregion
}