[英]Raise event from Inner class to outer class in c#
我在项目中使用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中创建了Event,如下所示:
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个属性,而InnerClass中有6个属性。
我会在内部类属性的设置器中添加事件触发器。 然后在父类的构造函数中,将IClass = new InnerClass()移入构造函数并附加事件侦听器。
由于您是MVVM,因此可以利用INotifyPropertyChanged,但从长期来看,热量会变得混乱。
对于要通知给父类的每个属性,最好有一个'PropertyName'Changed事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.