0

I have an animation with this trigger setup in LoadingAnimation.xaml:

<UserControl.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard x:Name="ProgressAnimation_BeginStoryboard" Storyboard="{StaticResource ProgressAnimation}"/>
        </EventTrigger>
</UserControl.Triggers>

In my MainWindow.xaml, I have this inside a grid:

<control:LoadingAnimation x:Name="loadingAnimation" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.Row="7" Grid.ColumnSpan="2" />

I understand that I can start the storyboard with the following code:

Storyboard storyboard = Application.Current.MainWindow.FindResource("ProgressAnimation") as Storyboard;
storyboard.Begin();

But how can I use "FindResource() when my storyboard is in a different class?

David Klempfner
  • 8,700
  • 20
  • 73
  • 153

2 Answers2

0

Lot many questions asking for this,

<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();

get your Storyboard and call the begin method.

Sankarann
  • 2,625
  • 4
  • 22
  • 59
  • the xaml code with the storyboard is in a different class to the MainWindow.xaml/cs class. How can I find a resource in a different class? – David Klempfner Jan 23 '14 at 06:09
  • MainWindow.cs/xaml is in the root namespace, then in a subnamespace, I have the storyboard. How can I access it? The code you provided doesn't work. It says it cannot be found. – David Klempfner Jan 23 '14 at 07:10
  • Basically the LoadingAnimation class has the ProgressAnimation storyboard. So not sure how to start the storyboard, using the: – David Klempfner Jan 23 '14 at 07:13
  • Check the updated answer, by merging the resource file you can get the storyboard and you can start the storyboard there.. – Sankarann Jan 23 '14 at 07:27
  • An unhandled exception of type 'System.Windows.ResourceReferenceKeyNotFoundException' occurred in PresentationFramework.dll Additional information: 'ProgressAnimation' resource not found. – David Klempfner Jan 24 '14 at 02:53
  • ok I FindResource() works coz I wrote this: But it won't show up on the grid anymore. – David Klempfner Jan 24 '14 at 03:38
0

Worked it out. First I needed to define the loadingAnimation user control as a resource to be able to use the method FindResource(). Then after finding the resource, I needed to call FindResource() again to get the storyboard, since it was defined as a resource within loadingAnimation.

This was in my tag:

<Window.Resources>
        <control:LoadingAnimation x:Key="loadingAnimation" />
</Window.Resources>

and then to use it in the grid, I put this line of code in the tag:

<UserControl Content="{StaticResource loadingAnimation}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.Row="7" Grid.ColumnSpan="2" /> 

Then in C# I used this method to get access to the Storyboard:

private void beginStoryBoard()
        {
            UserControl loadingAnimation = Application.Current.MainWindow.FindResource("loadingAnimation") as UserControl;
            Storyboard storyboard = loadingAnimation.FindResource("ProgressAnimation") as Storyboard;
            storyboard.Begin();
        }
David Klempfner
  • 8,700
  • 20
  • 73
  • 153