私は実行時にコントロールを追加するアプリケーションを持っており、PlaySoundActionのEventTrigger(s)を添付します。 XAMLではこれは簡単ですが、オンザフライでコントロールを生成し、これらのトリガを追加する必要があるため、少し難しくなります。CodebehindのSystem.Windows.Interactivity.EventTriggerはいくつかのコントロールで動作しますが、他のコントロールでは機能しません。
私はprevious questionで尋ねられましたが、そこで途中で出ることができました。最近のテストでは、私が解決するためにできることを結論に至りましたが、欠けているものがあることを望んでいました。私はButtonControlでEventTriggerを動作させることができますが、RegularPolygonではなく、RegularPolygonの中にボタンを埋め込むのが簡単で、ContentControlではないので、結果を得ることができません。
これは、私は現在、コンテナ(キャンバス)に新しいRegularPolygonsを追加するため、この方法を持っているものである:私はすべての私のRegularPolygonsが画面に表示され見ることができます
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;
}
。私は、他のテストコントロールがリストされた場所でそれを参照しようとすることによって、サウンドファイルへのパスが動作することを確認するためにテストしました。 PlaySoundActionでEventTriggerを動作させることはできません。ボタンbの周りの行のコメントを外すと、追加されたボタンが表示され、サウンド再生を聞くためにタップします。
次のように私は、XAMLでPlaySoundAction EventTriggerで動作するようにするRegularPolygonを取得することができます。
<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の動作を妨げているものがありますか?