2016-03-18 69 views
0

有人可以告訴我我做了什麼錯誤嗎?視圖中的標籤即使更改Error.ErroMsg也不會更改。即使型號改變了mvvm的值,標籤也不會改變

XAML視圖

<Label 
    x:Name="ErrorMsg" 
    Content="{Binding Path=Error.ErrorMsg, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    Grid.Column="0" 
    HorizontalAlignment="Left" 
    VerticalAlignment="Bottom" 
    Width="200" 
    Height="26"/> 

視圖模型

private ErrorModel error = new ErrorModel(); 

public ErrorModel Error 
{ 
    get { return error; } 
    set { error = value; } 
} 

// This method will be called by a state machine using a delegate 
public void displayErrorMessage(string message) 
{ 
    Error.ErrorMsg = message; 
    CommandManager.InvalidateRequerySuggested(); 
    logger.Trace(" Successfully displayed the error message"); 
} 

模型

// ModelBase inherited by INotifyPropertyChanged and IDisposable 
class ErrorModel : ModelBase 
{ 
    private string errorMsg; 
    public string ErrorMsg 
    { 
     get { return errorMsg; } 
     set { errorMsg = value; OnPropertyChanged("ErrorMsg"); } 
    } 
    public ErrorModel() { 
     errorMsg = "TestHello"; 
    } 
} 

模型庫 公共類模型庫:INotifyPropertyChanged的,IDisposable的 { /// ///視圖模型基部的默認構造 /// 保護模型庫() { }

/// <summary> 
    /// Initialized property changed event handler 
    /// </summary> 
    public event PropertyChangedEventHandler PropertyChanged; 

    /// <summary> 
    /// This function will call by public setters of the model class 
    /// </summary> 
    /// <param name="propertyName">Specific property name that needs to identify unique model member</param> 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     // Initialize handler by using property changed event handler delegate 
     PropertyChangedEventHandler handler = this.PropertyChanged; 
     // Check that current handler is available or not 
     if (handler != null) 
     { 
      // Initialize property changed event args 
      var propertyChangedEventArgs = new PropertyChangedEventArgs(propertyName); 
      // Call back using property changed event handler 
      handler(this, propertyChangedEventArgs); 
     }  
    } 

    /// <summary> 
    /// This is calle when leaving the view model 
    /// </summary> 
    public void Dispose() 
    { 
     this.OnDispose(); 
    } 

    /// <summary> 
    /// Model dependent data is cleared here 
    /// </summary> 
    protected virtual void OnDispose() { } 
} 

//調用displayErrorMessage通過代理人

string cpDoesntWorkError =「充電點不可用」。 this.uiDelegator.showErrorMessage(cpDoesntWorkError);

GUIDelegator

public delegate void DisplayErrorMessageDelegate(string message);  

/// <summary> 
     /// This will register the ErrorMesssage delegate passed by the RCU_GUI 
     /// </summary> 
     /// <param name="del">ErrorMessageDelegate object which need to registered will be passed</param> 

     public void setDisplayErrorMessageDelegate(DisplayErrorMessageDelegate del) 
     { 
      displayErrorMessageDelegate = del; 
      logger.Trace("Error message delegate successfully set by RCU_GUI"); 
     } 

     /// <summary> 
     /// Call back to the displayErrorMessage which is in RCU_GUI 
     /// </summary> 
     /// <param name="message">Error message need to be displayed</param> 
     /// <returns></returns> 
     public void showErrorMessage(string message) 
     { 
      // If the error message delegate is not null 
      if (displayErrorMessageDelegate != null) 
      { 
       logger.Trace("Displaying the error message"); 
       displayErrorMessageDelegate(message); 
      } 
      else 
       logger.Trace("null display error message delegate"); 
     } 
+0

它看起來像,問題出在什麼地方'uiDelegator'以及它是如何調用'displayErrorMessage'(他們是否使用相同的視圖模型實例),因爲代碼的其餘部分看起來不錯。 –

+0

我已經添加了委託類的代碼。請幫助我在這裏找出問題。即使模型更改其屬性ErrorMsg,我也無法找出爲什麼它不更新視圖。 –

+0

您能否顯示View Model的完整代碼以及要調用'setDisplayErrorMessageDelegate'方法的地方? –

回答

2

你必須要知道的幾件事情:

  1. 不管你的方法OnPropertyChangedModelBase適當提高PropertyChanged事件

  2. 在你視圖模型在屬性Error您不通知視圖,此屬性更改。然後一些其他代碼可能會在調用displayErrorMessage方法之前分配新的ErrorModel實例。由於該視圖並不知道Error屬性發生了變化,所以它仍然顯示TestHello字符串。如果是這樣的話,你可以修改你的視圖模型

    public ErrorModel Error 
    { 
        get { return error; } 
        set { error = value; OnPropertyChanged("Error"); } 
    } 
    
  3. 你代表這是調用displayErrorMessage方法必須使用分配給視圖DataContext性質相同視圖模型對象實例。因爲如果沒有,那麼肯定View不會收到關於屬性更改的通知。

+0

YesPropertyChnaged方法被觸發,它觸發了PropertyChangedEventHandler。當我調試程序時,字符串值也成功地分配給了Error.ErrorMsg。我可以通知視圖,我的財產已經改變了嗎?我以爲它會自動發生。請幫助我 –

+0

添加'OnPropertyChanged(「錯誤」)方法調用給'Error'屬性的設置者 –

+0

我添加了它,但沒有工作 –