2012-12-01 75 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