有人可以告訴我我做了什麼錯誤嗎?視圖中的標籤即使更改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");
}
它看起來像,問題出在什麼地方'uiDelegator'以及它是如何調用'displayErrorMessage'(他們是否使用相同的視圖模型實例),因爲代碼的其餘部分看起來不錯。 –
我已經添加了委託類的代碼。請幫助我在這裏找出問題。即使模型更改其屬性ErrorMsg,我也無法找出爲什麼它不更新視圖。 –
您能否顯示View Model的完整代碼以及要調用'setDisplayErrorMessageDelegate'方法的地方? –