我在LoadingAnimation.xaml中设置了这个触发器设置的动画:
<UserControl.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard x:Name="ProgressAnimation_BeginStoryboard" Storyboard="{StaticResource ProgressAnimation}"/>
</EventTrigger>
</UserControl.Triggers>
在我的MainWindow.xaml中,我在网格中有这个:
<control:LoadingAnimation x:Name="loadingAnimation" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.Row="7" Grid.ColumnSpan="2" />
我知道我可以使用以下代码启动故事板:
Storyboard storyboard = Application.Current.MainWindow.FindResource("ProgressAnimation") as Storyboard;
storyboard.Begin();
但是当我的故事板在另一个类中时,如何使用“FindResource()”?
答案 0 :(得分:0)
很多问题要求,
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/AssembelyName;component/LoadingAnimation.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Storyboard storyboard = this.FindResource("ProgressAnimation") as Storyboard;// this - is the current window's instance
storyboard.Begin();
获取故事板并调用begin方法。
答案 1 :(得分:0)
解决了这个问题。 首先,我需要将loadingAnimation用户控件定义为能够使用FindResource()方法的资源。 然后在找到资源之后,我需要再次调用FindResource()来获取故事板,因为它被定义为loadingAnimation中的资源。
这是我的标签:
<Window.Resources>
<control:LoadingAnimation x:Key="loadingAnimation" />
</Window.Resources>
然后在网格中使用它,我将这行代码放在标记中:
<UserControl Content="{StaticResource loadingAnimation}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.Row="7" Grid.ColumnSpan="2" />
然后在C#中,我使用此方法来访问Storyboard:
private void beginStoryBoard()
{
UserControl loadingAnimation = Application.Current.MainWindow.FindResource("loadingAnimation") as UserControl;
Storyboard storyboard = loadingAnimation.FindResource("ProgressAnimation") as Storyboard;
storyboard.Begin();
}