Problem in Pausing a Storyboard in WPF ?
-
Hi, I'm gonna pause a Storyboard in WPF, so I've used below code :
Storyboard TheStoryboard;
//Constructor
public window
{
TheStoryboard = (Storyboard)this.FindResource("TheStoryboardName");
}private void MenuItemPause_Click(object sender, RoutedEventArgs e)
{
TheStoryboard.Pause();
}But nothing happen! What is the right way to do that ? Thanks.
-
Hi, I'm gonna pause a Storyboard in WPF, so I've used below code :
Storyboard TheStoryboard;
//Constructor
public window
{
TheStoryboard = (Storyboard)this.FindResource("TheStoryboardName");
}private void MenuItemPause_Click(object sender, RoutedEventArgs e)
{
TheStoryboard.Pause();
}But nothing happen! What is the right way to do that ? Thanks.
I think you should get a reference to the actual storyboard that's running. What you may be getting a reference to with the code above is that storyboard class, but not the instantiation that's actually running. In addition, I would suggest using the keyword "as" to perform the cast. Then check to see if the variable is null. The cast you currently use, if it does not work, will throw an exception. The keyword "as" simply returns null if the cast does not work. Also, look into "TryFindResource" as opposed to "FindResource". I believe it works the same way in that it will return null if the resource is not found. Again, check for null before using it. Both of these suggested changes will make your code, in general, more robust. Let me know if this helps. Blitz
-
I think you should get a reference to the actual storyboard that's running. What you may be getting a reference to with the code above is that storyboard class, but not the instantiation that's actually running. In addition, I would suggest using the keyword "as" to perform the cast. Then check to see if the variable is null. The cast you currently use, if it does not work, will throw an exception. The keyword "as" simply returns null if the cast does not work. Also, look into "TryFindResource" as opposed to "FindResource". I believe it works the same way in that it will return null if the resource is not found. Again, check for null before using it. Both of these suggested changes will make your code, in general, more robust. Let me know if this helps. Blitz