作者:天亮说晚安 | 来源:互联网 | 2023-09-23 13:19
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 个解决方案
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,因为您缺少事件的冒泡和隧道。但我不知道这是否有效,我没有尝试,因为我还不需要气泡或隧道。如果有人有更好的解决方案,我将非常感激阅读它。