【发布时间】:2015-12-12 19:20:46
【问题描述】:
我在 WPF 中使用 MVVM 灯。 Model 类的属性会随着底层数据访问层的变化而不断变化。
型号:
public class SBE_V1_Model : ViewModelBase
{
public SBE_V1_Model(String name)
{
Name = "MAIN." + name;
SetupClient();
}
private void SetupClient()
{
client = new ConnectionHelper(this);
client.Connect(Name);
}
public Boolean Output
{
get
{
return _Output;
}
set
{
if (value != this._Output)
{
Boolean oldValue = _Output;
_Output = value;
RaisePropertyChanged("Output", oldValue, value, true);
}
}
}
}
如果Output 属性发生变化,那么绑定将被通知,所以这是可行的。但是从知道新值的数据访问源更新属性的正确方法是什么?
public class ConnectionHelper : ViewModelBase
{
public Boolean Connect(String name)
{
Name = name;
tcClient = new TcAdsClient();
try
{
dataStream = new AdsStream(4);
binReader = new AdsBinaryReader(dataStream);
tcClient.Connect(851);
SetupADSNotifications();
return true;
}
catch (Exception ee)
{
return false;
}
}
private void tcClient_OnNotification(object sender, AdsNotificationEventArgs e)
{
String prop;
notifications.TryGetValue(e.NotificationHandle, out prop);
switch (prop)
{
case "Output":
Boolean b = binReader.ReadBoolean();
RaisePropertyChanged("Output", false,b, true);
break;
}
}
}
为什么connectionhelper中的RaisePropertyChanged调用不更新模型的属性?如果这是错误的方式,我应该设置某种监听器吗?
【问题讨论】:
标签: c# wpf mvvm inotifypropertychanged