【问题标题】:Broken binding without error没有错误的损坏绑定
【发布时间】:2012-11-19 10:54:27
【问题描述】:

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

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

我使用这些调试和跟踪绑定: http://www.beacosta.com/blog/?p=52 How to detect broken WPF Data binding?

跟踪如下:

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>

我的代码的相关部分如下。

Loginview.xaml:

<Window.DataContext>
    <local:LoginViewModel/>
</Window.DataContext>

...

<Button Content="Login"
        Command="{Binding LoginCommand, diagnostics:PresentationTraceSources.TraceLevel=High}"
        CommandParameter="Hello">

LoginViewModel.cs:

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());
    }
}

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
}

【问题讨论】:

  • 我假设您的所有课程在您的代码中都是“公开的”?
  • 不...它们具有默认可见性。我在另一个项目中尝试过本教程 (codeproject.com/Articles/126249/…),它可以工作。
  • 这是实际代码吗?它不会编译,这一行是错误的:MessageBox.Show(obj.ToString());

标签: wpf mvvm binding


【解决方案1】:

您的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
}

【讨论】:

  • 是的,这就是问题所在!谢谢,真是个蹩脚的错误!然而,我仍然没有得到的是跟踪消息...... TransferValue - 产生的隐式转换器 是如此毫无意义。
猜你喜欢
  • 2013-04-17
  • 2016-01-28
  • 2012-08-26
  • 2010-09-18
  • 2012-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2015-07-19
相关资源
最近更新 更多