在c#中将事件从Inner类提升到外部类

时间:2014-09-04 06:02:34

标签: c# events inner-classes

我在项目中使用MVVM模式。

我的班级设计是这样的:

Class Model : AbstractModel
{
   InnerClass Iclass = new InnerClass();

  Public String ModelProp1
  {
    get
    {
     return Iclass.prop1;
     }
    set
    {
      Iclass.prop1 = value;
    }
   }

public override void SetLabel(UInt16 value, int Index)
    {
        byte[] arr = BitConverter.GetBytes(value);
        this.Iclass.IclassConfig[Index].Label = arr[0];
    }

 public override string DateFormat
    {
        get { return Iclass.intlDate.ToString(); }
        set { Iclass.intlDate = Convert.ToByte(value); }
    }

}

 Class InnerClass
{
public byte intlDate
 {
   get { return this.intl_date; }
        set { this.intl_date = value;
        RaiseModelPropertiesChangedEvent(new ValueChangedEventArgs {     Parameter_dateformat = this.intlDate });
  }

 private JClassa  []channel_config = new JClass[2];
 public JClass[] IclassConfig
    {
        get { return this.channel_config; }
        set { this.channel_config = value; }
    }

}

Public JClass
{
 private byte  channel_label;
  public byte Label
 {
  get { return this.channel_label; }
    set { this.channel_label = value;}
 }

我从其他应用程序获取数据。更新的数据来自InnerClass属性,我希望将此更新的数据推送到Model类。

JClass属性的问题是如何解决事件,以便将更新的数据推送到模型类。

为此我在InnerClass中创建了这样的事件:

 public event EventHandler<ValueChangedEventArgs> ModelPropertiesChanged;
  public void RaiseModelPropertiesChangedEvent(ValueChangedEventArgs e)
    {
        if (ModelPropertiesChanged != null)
            ModelPropertiesChanged(this, e);
    }
   public class ValueChangedEventArgs : EventArgs
   { 
      public int Parameter_dateformat { get; set; }
       public int Parameter_channelLabel { get; set; }
    }

告诉我如何实现这一目标。因为我在Jclass中有4个属性,而6个属性是InnerClass。

1 个答案:

答案 0 :(得分:0)

我会在内部类属性的setter中添加事件触发器。然后在父类的构造函数中,将IClass = new InnerClass()移动到构造函数中并附加事件侦听器。

由于你是MVVM,你可以利用INotifyPropertyChanged,但从长远来看,热量会变得混乱。

最好为要通知父类的每个属性设置一个&#39; PropertyName&#39; Changed事件。