【问题标题】:PropertyChanged WPF MVVM LightPropertyChanged WPF MVVM 灯光
【发布时间】: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


    【解决方案1】:

    在您的 SBE_V1_Model 类中,您应该订阅接收来自 ConnectionHelper ViewModel 的 PropertyChange 通知。

    // Attach EventHandler
    ConnectionHelper.PropertyChanged += OnConnectionHelperPropertyChanged;
    
    ...
    
    // When property gets changed, raise the PropertyChanged 
    // event of the ViewModel copy of the property
    OnConnectionHelperPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Something") //your ConnectionHelper property name
            RaisePropertyChanged("Ouput");
    }
    

    还可以查看 MVVM light messenger。这是您可能对 StackOverflow 感兴趣的link

    【讨论】:

      【解决方案2】:

      您应该只在 ViewModel 中使用 PropertyChanged,而不是在 Model 中。 您只能在特殊时期在模型中使用 PropertyChanged。

      RaisePropertyChanged("Output", false,b, true);
      

      在那个 PropertyChanged 你总是说输出属性被改变了。

      我建议你实现 INotifyPropertyChanged

      class MyClass : INotifyPropertyChanged
          {
            public bool MyProperty{ get; set; }
          
            public event PropertyChangedEventHandler PropertyChanged;
          
            protected void OnPropertyChanged(string name)
            {
              PropertyChangedEventHandler handler = PropertyChanged;
              if (handler != null)
              {
                handler(this, new PropertyChangedEventArgs(name));
               }
            }
          
          }
      

      要通知您必须使用的任何属性更改:

      OnPropertyChanged("MyProperty");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多