多次使用 WPF DoubleAnimation(ReSharper 在 XAML 中显示错误)
Use WPF DoubleAnimation multiple times (ReSharper shows error in XAML)
我想多次使用 DoubleAnimation。这里的代码:
<Window x:Class="Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<DoubleAnimation x:Key="LeftAnimation" Storyboard.TargetProperty="(Canvas.Left)" From="0" To="100" />
<DoubleAnimation x:Key="TopAnimation" Storyboard.TargetProperty="(Canvas.Top)" From="0" To="100" />
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="AnimatedBorder1">
<StaticResourceExtension ResourceKey="LeftAnimation" />
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="AnimatedBorder2">
<StaticResourceExtension ResourceKey="LeftAnimation" />
<StaticResourceExtension ResourceKey="TopAnimation" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Canvas>
<Border x:Name="AnimatedBorder1" Background="Blue" Width="20" Height="20" />
<Border x:Name="AnimatedBorder2" Background="Red" Width="20" Height="20" />
</Canvas>
</Window>
代码运行良好,但 ReSharper 在行 <StaticResourceExtension ResourceKey="LeftAnimation" />
中对 LeftAnimation 下划线并表示:“无效资源类型:预期类型为 'TriggerCollection', 实际类型是 'DoubleAnimation'."
这真的是个问题还是 ReSharper(版本 9.2)中的错误?
这是经过编辑的解决方案:
资源:
<Window.Resources>
<TimelineCollection x:Key="TimelinesCollectionKey1">
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" From="0" To="100" />
</TimelineCollection>
<TimelineCollection x:Key="TimelinesCollectionKey2">
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" From="0" To="100" />
</TimelineCollection></Window.Resources>
触发器:
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="AnimatedBorder1">
<!--<StaticResourceExtension ResourceKey="LeftAnimation" />-->
<Storyboard.Children>
<ParallelTimeline Children="{StaticResource TimelinesCollectionKey1}"></ParallelTimeline>
</Storyboard.Children>
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="AnimatedBorder2">
<Storyboard.Children>
<ParallelTimeline Children="{StaticResource TimelinesCollectionKey1}"></ParallelTimeline>
<ParallelTimeline Children="{StaticResource TimelinesCollectionKey2}"></ParallelTimeline>
</Storyboard.Children>
</Storyboard>
</BeginStoryboard>
</EventTrigger></Window.Triggers>
目标:
<Canvas>
<Border x:Name="AnimatedBorder1" Background="Blue" Width="20" Height="20" />
<Border x:Name="AnimatedBorder2" Background="Red" Width="20" Height="20" /></Canvas>
此致,
我想多次使用 DoubleAnimation。这里的代码:
<Window x:Class="Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<DoubleAnimation x:Key="LeftAnimation" Storyboard.TargetProperty="(Canvas.Left)" From="0" To="100" />
<DoubleAnimation x:Key="TopAnimation" Storyboard.TargetProperty="(Canvas.Top)" From="0" To="100" />
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="AnimatedBorder1">
<StaticResourceExtension ResourceKey="LeftAnimation" />
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="AnimatedBorder2">
<StaticResourceExtension ResourceKey="LeftAnimation" />
<StaticResourceExtension ResourceKey="TopAnimation" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Canvas>
<Border x:Name="AnimatedBorder1" Background="Blue" Width="20" Height="20" />
<Border x:Name="AnimatedBorder2" Background="Red" Width="20" Height="20" />
</Canvas>
</Window>
代码运行良好,但 ReSharper 在行 <StaticResourceExtension ResourceKey="LeftAnimation" />
中对 LeftAnimation 下划线并表示:“无效资源类型:预期类型为 'TriggerCollection', 实际类型是 'DoubleAnimation'."
这真的是个问题还是 ReSharper(版本 9.2)中的错误?
这是经过编辑的解决方案:
资源:
<Window.Resources> <TimelineCollection x:Key="TimelinesCollectionKey1"> <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" From="0" To="100" /> </TimelineCollection> <TimelineCollection x:Key="TimelinesCollectionKey2"> <DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" From="0" To="100" /> </TimelineCollection></Window.Resources>
触发器:
<Window.Triggers> <EventTrigger RoutedEvent="Window.Loaded"> <BeginStoryboard> <Storyboard Storyboard.TargetName="AnimatedBorder1"> <!--<StaticResourceExtension ResourceKey="LeftAnimation" />--> <Storyboard.Children> <ParallelTimeline Children="{StaticResource TimelinesCollectionKey1}"></ParallelTimeline> </Storyboard.Children> </Storyboard> </BeginStoryboard> <BeginStoryboard> <Storyboard Storyboard.TargetName="AnimatedBorder2"> <Storyboard.Children> <ParallelTimeline Children="{StaticResource TimelinesCollectionKey1}"></ParallelTimeline> <ParallelTimeline Children="{StaticResource TimelinesCollectionKey2}"></ParallelTimeline> </Storyboard.Children> </Storyboard> </BeginStoryboard> </EventTrigger></Window.Triggers>
目标:
<Canvas> <Border x:Name="AnimatedBorder1" Background="Blue" Width="20" Height="20" /> <Border x:Name="AnimatedBorder2" Background="Red" Width="20" Height="20" /></Canvas>
此致,