public sealed partial class FoodItemControl : UserControl{ public event EventHandler<StringEventArgs> thumbnailClicked; public FoodItemControl() { InitializeComponent(); (this.Content as FrameworkElement).DataContext = this; } private voID thumbnail_Tapped(object sender,TappedRoutedEventArgs e) { var handler = thumbnailClicked; if (handler != null) { handler(this,new StringEventArgs()); } }}
但是当我在xaml中为它分配事件时,无法找到暴露的事件处理程序.即
<local:FoodItemControl thumbnailClicked="SOMETHING" />
我在发现的例子中遗漏了什么吗?
编辑:我的问题似乎是我将事件定义为EventHandler< StringEventArgs>.一旦我将其改为简单的EventHandler I.e
public event EventHandler thumbnailedClicked;
但是我还是不明白为什么?
解决方法 您必须创建依赖项属性并注册该属性以在用户控件中公开它:public sealed partial class FoodItemControl : UserControl{ public EventHandler thumbnailClicked { get { return (EventHandler)GetValue(thumbnailClickedProperty); } set { SetValue(thumbnailClickedProperty,value); } } public static Readonly DependencyProperty thumbnailClickedProperty = DependencyProperty.Register("thumbnailClicked",typeof(EventHandler),typeof(FoodItemControl),new PropertyMetadata("")); public FoodItemControl() { this.InitializeComponent(); (this.Content as FrameworkElement).DataContext = this; }}总结
以上是内存溢出为你收集整理的c# – 在Custom Usercontrol – Window Store App中公开事件全部内容,希望文章能够帮你解决c# – 在Custom Usercontrol – Window Store App中公开事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)