热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

构建支持我自己定制事件的事件触发行为。-BuildingaEventTriggerBehaviorthatsupportsmyowncustomizedevents

IbuiltmyownUserControl(thatIconsumefromaseparateassembly)withitsowncustomizedevents,

I built my own UserControl (that I consume from a separate assembly) with its own customized events, in the parent project I want to use those events with Commands since it's an MVVM application, all by using the Behaviors SDK (XAML) for Visual Studio 2013, as follows :

我用自己定制的事件构建了自己的UserControl(我从另一个程序集中使用它),在父项目中,我希望使用这些事件和命令,因为它是一个MVVM应用程序,所有这些都是通过Visual Studio 2013的行为SDK (XAML)实现的,如下所示:


            
                
                    
                
            

The problem is that the EventTriggerBehavior supports only some specific events as mentioned here, hence the commands I'm using on my events don't fire, now, what I want to do is, to build behaviors that provide the same experience, and that support my own event: I want them to look similar to this :

问题是EventTriggerBehavior支持这里只提到一些具体的事件,因此,命令我使用我的事件不火,现在,我要做的是,建立行为提供相同的经验,和支持自己的事件:我希望他们类似于:


            
                
                    
                
            

I've read many articles about implementing such behaviors, but I couldn't get it clear for me since it's the first time I deal with them this way, I would like if you show me how to do.

我读过很多关于实现这种行为的文章,但是我不太清楚,因为这是我第一次用这种方式处理这些行为,我希望你能告诉我怎么做。

1 个解决方案

#1


0  

First of all you would normaly not add the behavior inside the UserControls XAML Page, you would add the Behavior on the XAML Page where you use the UserControl, so i think you will need something like this:

首先,你通常不会在UserControls XAML页面中添加行为,你会在使用UserControl的XAML页面中添加行为,所以我认为你需要这样的东西:

You can simply add a public event of type EventHandler e.g. "ForwardButtonClicked" to your Codebehind of your UserControl:

你可以简单地添加一个像EventHandler这样的公共事件。“forward dbuttonclick”到您的UserControl后面的代码:

public event EventHandler ForwardButtonClicked;

Then you must add a EventHandler Method too, to get your event fired when the button is clicked on the view. So you would add a eventhandlermethod inside the constructor of your usercontrol:

然后,您还必须添加一个EventHandler方法,以便在单击视图上的按钮时触发事件。你可以在usercontrol的构造函数中添加一个eventhandlermethod:

public PlayerControl()
{
  this.InitializeComponent();
  _forwardButton.Click += this.ForwardButtonClickedEventHandler;
}

The Eventhandler would look like:

晚上的人会是这样的:

private void ForwardButtonClickedEventHandler(object sender, RoutedEventArgs eventArgs)
{
  if(this.ForwardButtonClicked != null)
  {
    this.ForwardButtonClicked.Invoke(sender, new EventArgs());
  } 
}

Then you can add your UserControl to a XAML Page and add a EventTriggerBehavior for the ForwardButtonClicked Event of your UserControl. Since there is no support for Custom RoutedEvents in WinRT you may use a default clicked RoutedEventHandler instead of a normal C# EventHandler because you lack bubbleing and tunneling of events. But i don't know if this is working, i did not try it because i did not need bubbleing or tunneling yet. If someone has a better solution, i would much appreciate to read about it.

然后,您可以将您的UserControl添加到XAML页面,并为您的UserControl的forwardbuttonclick事件添加EventTriggerBehavior。因为在WinRT中没有对自定义的RoutedEvents的支持,所以您可能会使用默认的RoutedEventHandler,而不是正常的c# EventHandler,因为您缺少事件的冒泡和隧道。但我不知道这是否有效,我没有尝试,因为我还不需要气泡或隧道。如果有人有更好的解决方案,我将非常感激阅读它。


推荐阅读
  • 本文介绍如何使用布局文件在Android应用中排列多行TextView和Button,使其占据屏幕的特定比例,并提供示例代码以帮助理解和实现。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 机器学习中的相似度度量与模型优化
    本文探讨了机器学习中常见的相似度度量方法,包括余弦相似度、欧氏距离和马氏距离,并详细介绍了如何通过选择合适的模型复杂度和正则化来提高模型的泛化能力。此外,文章还涵盖了模型评估的各种方法和指标,以及不同分类器的工作原理和应用场景。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 尽管使用TensorFlow和PyTorch等成熟框架可以显著降低实现递归神经网络(RNN)的门槛,但对于初学者来说,理解其底层原理至关重要。本文将引导您使用NumPy从头构建一个用于自然语言处理(NLP)的RNN模型。 ... [详细]
  • MySQL索引详解与优化
    本文深入探讨了MySQL中的索引机制,包括索引的基本概念、优势与劣势、分类及其实现原理,并详细介绍了索引的使用场景和优化技巧。通过具体示例,帮助读者更好地理解和应用索引以提升数据库性能。 ... [详细]
  • 本文探讨了《魔兽世界》中红蓝两方阵营在备战阶段的策略与实现方法,通过代码展示了双方如何根据资源和兵种特性进行战士生产。 ... [详细]
  • 本题探讨如何通过最大流算法解决农场排水系统的设计问题。题目要求计算从水源点到汇合点的最大水流速率,使用经典的EK(Edmonds-Karp)和Dinic算法进行求解。 ... [详细]
  • 创建项目:Visual Studio Online 入门指南
    本文介绍如何使用微软的 Visual Studio Online(VSO)创建和管理开发项目。作为一款基于云计算的开发平台,VSO 提供了丰富的工具和服务,简化了项目的配置和部署流程。 ... [详细]
  • Coursera ML 机器学习
    2019独角兽企业重金招聘Python工程师标准线性回归算法计算过程CostFunction梯度下降算法多变量回归![选择特征](https:static.oschina.n ... [详细]
author-avatar
天亮说晚安
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有