Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 从WP7中的自定义控件在页面中执行一些代码_Silverlight_Windows Phone 7_User Controls_Custom Controls - Fatal编程技术网

Silverlight 从WP7中的自定义控件在页面中执行一些代码

Silverlight 从WP7中的自定义控件在页面中执行一些代码,silverlight,windows-phone-7,user-controls,custom-controls,Silverlight,Windows Phone 7,User Controls,Custom Controls,我已经创建了一个自定义用户控件。如果我在名为Page1的页面中创建了此控件的实例。我需要一些代码来执行在第1页根据一些行动内的用户控制。如何实现这一点?在用户控件中,定义一个事件: public event EventHandler MyEvent; 在页面中,钩住该事件: void Page_Load(...) { userControl.MyEvent += MyCode; } 在控件中,当事件希望在页面上运行代码时,执行该事件: void SomethingHappened() {

我已经创建了一个自定义用户控件。如果我在名为Page1的页面中创建了此控件的实例。我需要一些代码来执行在第1页根据一些行动内的用户控制。如何实现这一点?

在用户控件中,定义一个事件:

public event EventHandler MyEvent;
在页面中,钩住该事件:

void Page_Load(...)
{
  userControl.MyEvent += MyCode;
}
在控件中,当事件希望在页面上运行代码时,执行该事件:

void SomethingHappened()
{
  var e = MyEvent;
  if (e != null)
  {
    e(this, EventArgs.Empty);
  }
}

就这样。

你能在Page1中发布一些代码吗,例如控件的xaml源代码?它们都需要访问相同的代码/范围。一种方法是将它们都绑定到同一个viewmodel,以便usercontrol中的操作调用viewmodel中的某些内容,这些内容可以在页面上看到并反映出来。@igrali我没有使用MVVM。如果根据用户控件中自定义属性的更改在Page1中执行某些代码,那么我就满意了。谢谢