gpt4 book ai didi

wpf - 如何在 XAML 中使元素引用 StaticResource Storyboard(而不是 Storyboard 引用元素)

转载 作者:行者123 更新时间:2023-12-04 14:14:20 26 4
gpt4 key购买 nike

我正在阅读 MSDN 动画教程,它描述了将 Storyboard应用于元素的以下步骤:

  1. 创建 Storyboard;
  2. TargetName属性指定它的目标元素名称;
  3. (指定目标属性);
  4. (添加事件触发器以启动动画);

我看到一个概念性问题,从中得出我的困难,那就是:

I have a one-to-one relationship between storyboard and element, and this relationship is defined in the storyboard. Then, how could I create ONE storyboard, and conditionally apply it to more than one element, triggering the animation FROM THE ELEMENT ITSELF (via Binding / Triggers, I suppose).

我的预期用例是模拟一个 LED 面板(椭圆堆叠面板),其中每个 LED 可以处于四种逻辑状态之一:开、关、快闪和慢闪(非常像以太网路由器) .然后,我将创建动画 BlinkingSlowBlinkingFast,当我的 ViewModel 进入相应的逻辑状态时将触发它们。然后我可以只关心 ViewModel 中的行为,让 View 自行处理,适当触发和重用一些 StaticResource Storyboard。

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:blinking"
x:Class="blinking.MainWindow"
Title="MainWindow"
Background="{x:Null}"
WindowStartupLocation="CenterScreen">

<Window.Resources>
<System:Double x:Key="Diameter">40</System:Double>
<Color x:Key="RedOn">Red</Color>
<Color x:Key="RedOff">#FF570000</Color>
<Storyboard x:Key="BlinkSlow" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
Storyboard.TargetName="led4"
AutoReverse="True"
RepeatBehavior="Forever">
<DiscreteColorKeyFrame KeyTime="0" Value="{StaticResource RedOn}"/>
<DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="{StaticResource RedOn}"/>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="{StaticResource RedOff}"/>
<DiscreteColorKeyFrame KeyTime="0:0:1" Value="{StaticResource RedOff}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>

<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource BlinkSlow}"/>
</EventTrigger>
</Window.Triggers>

<StackPanel x:Name="leds_container" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,20,4,0">
<Ellipse x:Name="led1" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
<Ellipse x:Name="led2" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
<Ellipse x:Name="led3" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
<Ellipse x:Name="led4" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
</StackPanel>
</Window>

有什么建议吗?

最佳答案

您可以为此使用 Style-Triggers。

像您已经做的那样在资源部分创建 Storyboard,但没有目标名称。

然后您为您的椭圆创建一个样式,其中包含一个 DataTrigger,启动您当前状态所需的动画。

例如:

<Window.Resources>
<!--
Other declarations
-->
<Style TargetType="{x:Type Ellipse}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State, Mode=OneWay}" Value="BlinkSlow">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource BlinkSlow}" />
</DataTrigger.EnterActions>
</DataTrigger>
<!--
Add DataTrigger for your other states too.
-->
</Style.Triggers>
</Style>
</Window.Resources>

关于wpf - 如何在 XAML 中使元素引用 StaticResource Storyboard(而不是 Storyboard 引用元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16171422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com