[英]Access Parent level object
public class Activity
{
public games _Games {get;set;}
public sports _Sports {get;set;}
}
public class games : PropertyChangedBase
{
public int player
{
get;
set; //have if- else statement
}
}
public class sports : PropertyChangedBase
{
public int sub{get;set;}
}
Aim: when the games player is more than 2, I would like to update sports sub variable to 10. 目的:当游戏玩家大于2时,我想将sports子变量更新为10。
Question: How can I access the parent class and update the sports class variable? 问题:如何访问父类并更新运动类变量?
You need to create an instance of Activity
. 您需要创建一个
Activity
实例。 You also need to initialize _Sports
in it 您还需要在其中初始化
_Sports
Activity activity = new Activity();
activity._Sports = new sports();
activity._Sports.sub = 10;
Or using object tantalizer 或使用对象诱剂
Activity activity = new Activity
{
_Sports = new sports()
};
activity._Sports.sub = 10;
By the way, Activity
is not parent class of sports
. 顺便说一句,
Activity
不是sports
父类。 Activity
holds sports
object as a member. Activity
将sports
对象作为成员。 In your example PropertyChangedBase
is parent class of games
. 在您的示例中
PropertyChangedBase
是games
父类。
You could use an event that would signal to the Activity
class that it is time to update. 您可以使用一个事件,该事件将向
Activity
类发出信号,表明该更新了。
public class games
{
public event UpdatePlayerSubDelegate UpdatePlayerSub;
public delegate void UpdatePlayerSubDelegate();
private int _player;
public int player
{
get { return _player; }
set
{
_player = value;
if (_player > 2)
{
// Fire the Event that it is time to update
UpdatePlayerSub();
}
}
}
}
In the Activity class you can register the event in the constructor and write in to the event handler the necessary update. 在Activity类中,您可以在构造函数中注册事件,并将必要的更新写入事件处理程序。 In your case sub to 10:
在您的情况下,请减去10:
public class Activity
{
public games _Games { get; set; }
public sports _Sports { get; set; }
public Activity()
{
this._Games = new games();
this._Games.UpdatePlayerSub += _Games_UpdatePlayerSub;
this._Sports = new sports();
}
private void _Games_UpdatePlayerSub()
{
if (_Sports != null)
{
_Sports.sub = 10;
}
}
}
EDIT I just saw the tag INotifyPropertyChanged
. 编辑我刚刚看到标签
INotifyPropertyChanged
。 Of course you could also use this interface and the provided event. 当然,您也可以使用此接口和提供的事件。 Implement the interface as the following:
如下实现接口:
public class games : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _player;
public int player
{
get { return _player; }
set
{
_player = value;
if (_player > 2)
{
// Fire the Event that it is time to update
PropertyChanged(this, new PropertyChangedEventArgs("player"));
}
}
}
}
And in the Activity
class register again to the event in the constructor: 然后在
Activity
类中再次向构造函数中的事件注册:
public Activity()
{
this._Games = new games();
this._Games.PropertyChanged += _Games_PropertyChanged;
this._Sports = new sports();
}
and declare the body of the event handler: 并声明事件处理程序的主体:
private void _Games_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (_Sports != null)
{
_Sports.sub = 10;
}
}
And _Sports.sub
will get updated automatically. _Sports.sub
将自动更新。 Hope it helps. 希望能帮助到你。 There are of course other ways to accomplish this update.
当然,还有其他方法可以完成此更新。 It is just the first the came to my mind
这只是我想到的第一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.