2012-12-01 54 views
2

我有两个窗口的简单WPF应用程序,我想改变它使用MVVM模型。损坏的绑定没有错误

我开始创建一个按钮和一个消息框的简单绑定,但它不起作用。我的命令的执行功能从来没有被调用,但是在输出窗口中没有绑定错误。

我调试和跟踪结合使用这些: 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 
} 
+0

我假设你的代码中所有的类都是“公共的”? –

+0

不...他们有默认的知名度。 我试过这个教程(http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute)在一个不同的项目,它的工作原理。 – hthms

+0

这是实际的代码?它不会编译,这一行是错误的:MessageBox.Show(obj.ToString()); – dzavala

回答

2

你的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 
} 
+0

是的,这是问题!谢谢,这样的蹩脚错误! 但是我仍然没有得到的是跟踪消息... TransferValue - 生成的隐式转换器是如此毫无意义。 – hthms