后台代码中的system . windows . interactiy.eventtrigger在某些控件上有效,但在其
本文关键字:控件 有效 后台 system windows eventtrigger interactiy 代码 | 更新日期: 2023-09-27 18:19:10
我有一个应用程序,我在运行时添加控件,并希望附加EventTrigger(s)是PlaySoundAction。在XAML中,这很容易,但由于需要动态生成控件并添加这些触发器,这就变得有点困难了。
我在之前的问题中问过,并且能够在那里得到部分方法,并且通过最近的测试,我已经得出了关于我可以做些什么来解决它的结论,但希望可能有一些我错过的东西。我可以让EventTrigger在按钮控件上工作,但不能在RegularPolygon上工作,我无法在RegularPolygon内嵌入按钮以实现我想要的结果,因为它不是ContentControl。
这是我目前有这个方法添加新的正则多边形到一个容器(画布):
public RegularPolygon AddShape(Panel container)
{
RegularPolygon shape = new RegularPolygon();
// ShapeColors is a List<SolidColorBrush> that has been prepopulated
// _random is a Random() from .NET
shape.Fill = ShapeColors[_random.Next(0, ColorCount - 1)];
shape.Stretch = Stretch.Fill;
shape.PointCount = _random.Next(3, 6);
shape.UseLayoutRounding = false;
shape.Stroke = new SolidColorBrush(Colors.Black);
shape.Width = _random.Next(50, 150);
shape.Height = _random.Next(50, 150);
// UI_Tap is an arbitrary function for handling the Tap event
shape.Tap += UI_Tap;
System.Windows.Interactivity.EventTrigger trigger = new System.Windows.Interactivity.EventTrigger("Tap");
PlaySoundAction correct = new PlaySoundAction();
correct.Source = new Uri("/Sounds/Correct.mp3", UriKind.RelativeOrAbsolute);
correct.Volume = 1;
trigger.Actions.Add(correct);
//Button b = new Button();
//b.Content = "This is button B";
//System.Windows.Interactivity.Interaction.GetTriggers(b).Add(trigger);
//container.Children.Add(b);
// Comment this line out when uncommenting Button b block because Triggers
// can only be associated with one element
System.Windows.Interactivity.Interaction.GetTriggers(shape).Add(trigger);
Canvas.SetLeft(shape, _random.Next((int)(shape.Width / 2), (int)(container.Width - (shape.Width / 2))));
Canvas.SetTop(shape, _random.Next((int)(shape.Height / 2), (int)(container.Height - (shape.Height / 2))));
container.Children.Add(shape);
return shape;
}
我可以看到我所有的正则多边形出现在屏幕上。我已经测试了,以确保路径到声音文件工作,让其他测试控件试图引用它在列出的位置。让EventTrigger与PlaySoundAction一起工作是没有运气的。如果我取消注释按钮b周围的行,我可以看到添加的按钮,并点击它来听到声音播放。
我可以得到一个正则多边形与一个PlaySoundAction事件触发器在XAML工作,如下所示:
<es:RegularPolygon x:Name="haxx" Fill="#FFF4F4F5" Height="100" InnerRadius="1" Canvas.Left="125" PointCount="5" Stretch="Fill" Stroke="Black" Canvas.Top="187" UseLayoutRounding="False" Width="100">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<eim:PlaySoundAction Source="/Sounds/Correct.mp3"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</es:RegularPolygon>
我接下来的解决方案是创建一个自定义控件,让我使用RegularPolygon的形状和按钮的功能,因为我可以从代码隐藏中得到它。是否有什么我错过了,这是阻止我有这个EventTrigger工作?
这是一个问题中没有足够信息的情况。UI_Tap(…)方法有几个目标:
- 当RegularPolygon被点击时播放声音
- 确定是否需要在屏幕上绘制更多对象(以避免没有东西可触摸)
- 从父容器中移除Tap'd RegularPolygon
得到的是从父容器中删除对象,它并没有立即击中我,我的对象将被阻止执行与之相关的EventTrigger。这也是为什么在主页上硬编码一个正则多边形的原因。xaml工作得和上面的按钮一样好,因为我从来没有试图在Tap事件处理程序中删除它们。
我有一些建议,将PlaySoundAction的EventTrigger与ManipulationStartedEvent绑定,因为它发生在Tap事件之前,这将起作用,但我将采用更复杂的路线,使用XNA库在更新循环期间播放SoundEffect。它涉及到创建一个IApplicationLifetimeService,并在FrameDispatchTimer_Tick事件中排队播放声音,如下所示:
FrameworkDispatcher。