FindResource() not finding resource defined in XAML
-
Wednesday, April 18, 2007 8:12 PM
Hello everyone.
I am a relative n00b with WPF (and somewhat with .NET in general) with a rather tight deadline on a WPF project and have hit a wall with FindResource(). I am at the beginning stages of development so the setup of my application is still fairly simple. I have the following structure in my XAML code:
Code Snippet<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MIX_Presentation_2.Window1"
x:Name="Window"
Title="Window1"
Width="640"
Height="480"
KeyDown="goToNext"><Grid x:Name="LayoutRoot">
<Viewbox x:Name="ZAM3DViewbox"
ClipToBounds="true"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
xmlns:c="http://schemas.openxmlformats.org/markup-compatibility/2006" c:Ignorable="d">>
<Viewbox.Resources>
<Storyboard Duration="Forever"
FillBehavior="HoldEnd"
BeginTime="0:0:0"
x:Name="Storyboard1"
x:Key="Storyboard1"
d:StoryboardName="Storyboard1">
<Vector3DAnimationUsingKeyFrames RepeatBehavior="Forever"
BeginTime="0:0:0"
Duration="0:0:7.3"
Storyboard.TargetName="Free_Camera01OR8"
Storyboard.TargetProperty="(PerspectiveCamera.LookDirection)">
... keyframes ...
</Vector3DAnimationUsingKeyFrames>
</Storyboard>
</Viewbox.Resources>
</Viewbox>
</Grid>
</Window>
This is my C# code. This method is called by the KeyDown event handler for the Window container.
Code Snippetprivate int stepIndex = 0;
public
void goToNext(object sender, RoutedEventArgs e){
try { Storyboard sb = (Storyboard) Application.Current.FindResource("Storyboard1");}
catch(Exception ex){
MessageBox.Show(ex.ToString());}
sb.BeginAnimation();
stepIndex++;}
All I am trying to do for now is start the animation defined in the storyboard, however I have not yet had any luck even finding the storyboard resource. I have tried just FindResource and this.FindResource also in addition to Application.Current.FindResource. This function is defined in the .cs file for the XAML file shown above.
Any help you fine folks could offer would be appreciated. If I have left anything out or you need more information from me feel free to holler.
TIA
All Replies
-
Wednesday, April 18, 2007 8:29 PM
Also, I have tried the method
Code SnippetStoryboard sb = Window.Resources["Storyboard1"] as Storyboard;
... as well. In every attempt so far sb is returned as null.
-
Wednesday, April 18, 2007 8:30 PM
That won't work because FindResource "walks up" the resource tree, not down. You are asking the Application for a resource, but the actual resource itself is in a Viewbox in a Window. You need to call FindResource on that Viewbox, or a descendant of it.
You aren't kidding about a tight deadline...MIX is just a week or so away!

- Proposed As Answer by ups2 Friday, July 09, 2010 12:24 PM
- Marked As Answer by Sheldon _XiaoModerator Wednesday, July 20, 2011 6:22 AM
-
Wednesday, April 18, 2007 8:47 PM
Perfect. I used the method on the viewbox instead of the window and it found it. Thanks!
And yes, I have a week or so of long days ahead no doubt, wish me luck.
-
Wednesday, April 18, 2007 9:37 PM
Brett,
I'm glad that worked out for you.
BTW - you're supposed to mark the post which answered the question as The Answer, not the post which acknowledges it.

Josh

