Windows Phone 7上的UserControl上的XAML中的自定义事件 [英] Custom events in XAML on my UserControl on Windows Phone 7

查看:151
本文介绍了Windows Phone 7上的UserControl上的XAML中的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows Phone 7中创建一个UserControl,而当用户点击Ok按钮时,其他使用UserControl的XAML可以添加与之相关的事件。



使用一个例子,就像这样:



我有我的MainPage.xaml,我正在使用我的UserControl,所以它是像:

 < local:MyUserControl Canvas.Top =0x:Name =lSelectorWidth =480Height =800值=0000/> 

值只是我创建的DependencyProperty。我想要的是能够这样做:

 < local:MyUserControl Canvas.Top =0x :Name =lSelectorWidth =480Height =800Value =0000ValueChanged =lSelector_ValueChanged/> 

我该怎么做?

解决方案

将事件添加到您的UserControl像下面的代码,它将像普通事件一样出现

  public局部类UserControl1:UserControl 
{
public delegate void ValueChangedEventHandler(object sender,EventArgs e);

public event ValueChangedEventHandler ValueChanged;

public UserControl1()
{
//初始化变量必需
InitializeComponent();
}

private void Button_Click(object sender,System.Windows.RoutedEventArgs e)
{
if(ValueChanged!= null)
{
ValueChanged(this,EventArgs.Empty);
}
}
}



然后只订阅

  private void UserControl1_ValueChanged(object sender,System.EventArgs e)
{
// TODO:在这里添加事件处理程序实现。
}


I'm making a UserControl in Windows Phone 7 and i want that when the user clicks on an Ok button the other XAMLs which are using my UserControl can be able to add an event related to that.

Using an example it's like this:

I have my MainPage.xaml and i'm using my UserControl there, so it's something like:

<local:MyUserControl Canvas.Top="0" x:Name="lSelector" Width="480" Height="800" Value="0000"/>

Value is just a DependencyProperty that i created. What i want is to be able to do something like this:

<local:MyUserControl Canvas.Top="0" x:Name="lSelector" Width="480" Height="800" Value="0000" ValueChanged="lSelector_ValueChanged"/>

how can i do that?

解决方案

Add event to your UserControl like code below and it will appears like normal event

    public partial class UserControl1 : UserControl
    {
       public delegate void ValueChangedEventHandler(object sender, EventArgs e);

       public event ValueChangedEventHandler ValueChanged;

       public UserControl1()
       {
           // Required to initialize variables
           InitializeComponent();
       }

       private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
       {
          if (ValueChanged != null)
          {
              ValueChanged(this, EventArgs.Empty);
          }
       }
   }

Then just subscribe to it

   private void UserControl1_ValueChanged(object sender, System.EventArgs e)
    {
        // TODO: Add event handler implementation here.
    }

这篇关于Windows Phone 7上的UserControl上的XAML中的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆