我有一个用户控件,它以win形式添加(它会动态添加)。在另一个类中我有属性,我有一个该属性的事件,当它更新时,通知该项目的其他部分案件 通知用户控件。但我的问题是Usercontrol永远不会被通知。我可以看到属性正确获取更新,但委托类型的事件始终为null。
这是我的代表类型
public delegate void Submited(bool value);
我有一个用户控件,它被添加到表单中,然后在单击按钮时加载该表单。 下面是我如何添加用户控件到表单
UserDoc UserDocUserControl= new UserDoc(); //This is user control
UserDocForm UserDocWinForm = new UserDocForm(); //This is Form
UserDocWinForm.Controls.Add(UserDocUserControl);
UserDocWinForm.Show();
这是我的UserDoc用户控件
public partial class UserDoc : UserControl
{
GetUserAction Var_GetUserAction = new GetUserAction();
public UserDoc()
{
InitializeComponent();
Var_GetUserAction.DocSubmitted += OnDocSubmitted;
}
private void OnDocSubmitted(bool value)
{
// it never get hits
MessageBox.Show("Event Caught");
}
/// More code which is not making any difference here.........
}
以下是我的班级关注财产
public class GetUserAction
{
public event Submited DocSubmitted;
private bool _IsAllowed
public bool IsAllowed
{
get { return _IsAllowed; }
set
{
// _IsAllowed gets hit when ever the value is changed
if (_IsAllowed != value)
{
_IsAllowed = value;
// this DocSubmitted is always Null
if (DocSubmitted != null)
{
//It never comes here?????
DocSubmitted(value);
}
}
}
}
private void SetIsAllowed()
{
IsAllowed = // some other code is updating this part and its working fine;
}
}
任何指导/帮助都会受到赞赏,我无法弄清楚我做错了什么!!!
答案 0 :(得分:0)
正在更新的GetUserAction
对象可能与UserDoc
类引用的对象不同。您可能需要将GetUserAction
作为UserDoc
构造函数中的引用传递。
答案 1 :(得分:0)
你必须设置IsAllowed然后调用事件
添加:
public UserDoc()
{
InitializeComponent();
Var_GetUserAction.DocSubmitted += OnDocSubmitted;
//set properties to call event
Var_GetUserAction.IsAllowed = true;
}
你可以得到消息来源: https://drive.google.com/file/d/0B_JxRo8fyd3QbnBxVE5HdmF1bWs/view?usp=sharing