我有一个名为x:Name="wSplash"
的窗口,其中包含一个按钮x:Name="MyButton"
我在一个单独的页面中有一个ResourceDictionary
,其中包含一些Storyboards
。
我想使用 XAML
将ResourceDictionary
中的动画应用到wSplash
窗口中的按钮
我尝试了Storyboard.TargetName="wSplash.MyButton"
和Storyboard.TargetName="(wSplash.MyButton)"
,但我得到了一个例外:无法解析TargetName。
有没有办法用 XAML ?
来做到这一点wSplash.xaml:
<Window x:Class="wSplash" x:Name="wSplash"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="The Project" WindowStyle="None" ResizeMode="NoResize" AllowsTransparency="True"
WindowStartupLocation="CenterScreen" Height="350" Width="525" Background="Transparent">
<Border BorderBrush="#809FD3" Background="#222" BorderThickness="1" CornerRadius="5">
<Canvas>
<Button x:Name="MyButton" Content="Hello" />
</Canvas>
</Border>
</Window>
Animations.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Storyboard x:Key="AnSplash">
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)"
From="-15" To="13" Duration="0:0:0:01" />
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
From="-15" To="101" Duration="0:0:0:03" />
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
From="357" To="168" Duration="0:0:0:05" />
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)"
From="540" To="166" Duration="0:0:0:07" />
</Storyboard>
</ResourceDictionary>
wSplash.xaml.vb:
Public Sub MyEvent()
Dim x As Storyboard = FindResource("AnSplash")
x.Begin()
End Sub
当然,我将 Application.xaml 中的 Animations.xaml 包含为MergedDictionaries