?
Solved

Can't get Storyboard running

Posted on 2009-02-08
15
Medium Priority
?
1,272 Views
Last Modified: 2013-11-12
Hi, i'm having problem getting my animation being played in silverlight application. I have Style defined in my App.xaml and within two animations. When I associate Style everything works just fine, but when I wan't to play the animation i made i Blend - app crashes. I am accessing animation using this class:

     public static class ResourceLocator  
     {  
         /// <summary>  
         /// Helper method for finding resources located in app.xaml  
         /// </summary>  
         ///  
         ///<param name="name"></param>  
         /// <returns></returns>  
         public static object FindResource(string name)  
         {  
             if (App.Current.Resources.Contains(name))  
             {  
                 return App.Current.Resources[name];  
             }  
             else  
             {
                 FrameworkElement root = App.Current.RootVisual as FrameworkElement;
                 return root.FindResource(name);  
             }  
        }

        internal static object FindResource(this FrameworkElement root, string name)
        {
            if (root != null && root.Resources.Contains(name))
            {
                return root.Resources[name];
            }
            else
            {
                try
                {
                    return root.FindName(name);
                }
                catch { }
            }
            return null;
        }
     }

It returns null. What's wrong?
<Application.Resources>
        <Style x:Name="DangerZonePushpinStyle" TargetType="ProjectAfrica:DangerZonePushpin">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ProjectAfrica:DangerZonePushpin">
                        <Grid Height="15" Width="13" RenderTransformOrigin="0.479,0.456" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,0">
                            <Grid.Resources>
                                <ResourceDictionary>
                                <Storyboard x:Name="animMouseEnter">
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.Opacity)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.699999988079071"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF721400"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FF724900"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(Shape.Stroke).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF4F0000"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FF4E4F00"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                                <Storyboard x:Name="animMouseLeave">
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.Opacity)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0.699999988079071"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF724900"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FF721400"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(Shape.Stroke).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF4E4F00"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FF4F0000"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                                </ResourceDictionary>
                            </Grid.Resources>
                            <Grid.RenderTransform>
                                <ScaleTransform x:Name="_ScaleTransform" ScaleX="1" ScaleY="1" />
                            </Grid.RenderTransform>
                        	<Grid.RowDefinitions>
                        		<RowDefinition Height="*"/>
                        	</Grid.RowDefinitions>
                        	<Grid Margin="4.002,4.938,-0.002,0.062">
                        		<Ellipse Width="9.175" d:LayoutOverrides="Width" Stretch="Fill" StrokeMiterLimit="12" StrokeDashCap="Flat" Opacity="0.7" x:Name="ellipse">
                        			<Ellipse.Stroke>
                        				<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        					<GradientStop Color="#FF5E0000"/>
                        					<GradientStop Color="#FF4F0000" Offset="1"/>
                        				</LinearGradientBrush>
                        			</Ellipse.Stroke>
                        			<Ellipse.Fill>
                        				<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        					<GradientStop Color="#FF5B0000"/>
                        					<GradientStop Color="#FF721400" Offset="1"/>
                        				</LinearGradientBrush>
                        			</Ellipse.Fill>
                        		</Ellipse>
                        		<Ellipse Height="1.918" HorizontalAlignment="Right" Margin="0,2.343,1.971,0" Width="2.387" d:LayoutOverrides="HorizontalAlignment, Width, Height" Stroke="{x:Null}" Fill="#FFFFFFFF" Opacity="0.1" VerticalAlignment="Top"/>
                        	</Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>

Open in new window

0
Comment
Question by:zstapic
  • 7
  • 5
  • 3
15 Comments
 
LVL 16

Expert Comment

by:TSmooth
ID: 23590470
Because your animation is defined in a style that can be used for multiple elements, I believe that it loads in its own namescope which means you can't find any sub elements of it by name from the root element. You should however be able to find it by calling FindName() on the element to which this style is applied.
0
 
LVL 4

Author Comment

by:zstapic
ID: 23590627
I tried with this code:

(this.FindName("animMouseLeave") as Storyboard).Begin();

but I get the same Exception :(.
0
 
LVL 4

Author Comment

by:zstapic
ID: 23590650
Of course, i applied DangerZonePushpinStyle to the element this references.
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 16

Expert Comment

by:TSmooth
ID: 23591210
What is the exception you are getting? Are you sure "this" is the correct context? Basically what I'm saying is that if in your xaml you have something like this:

<ProjectAfrica:DangerZonePushPin x:Name="TestItem" Style="{StaticResource DangerZonePushpinStyle}" />

Then your code would need to look like:
Storyboard sb = TestItem.FindName("animMouseLeave") As Storyboard;
if(sb != null)
{
  sb.Begin();
}

Use debugging and see if it's even getting to the "sb.Begin()" line or if "sb" is null meaning it can't find "animMouseLeave".
0
 
LVL 4

Author Comment

by:zstapic
ID: 23592245
I get  Exception "Object reference not set to an instance of an object." which means it is null - it can't find "animMouseLeave". In your code it wouldn't get to the "sb.Begin()" method.
0
 
LVL 16

Expert Comment

by:TSmooth
ID: 23592567
Did you try my code though? I just want to make sure you're calling findname on the actual DangerZonePushPin object.
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 23592610
what is the exception you are getting?
0
 
LVL 4

Author Comment

by:zstapic
ID: 23593088
Yes, I used your code too (look at the code below). I'm  getting  Exception "Object reference not set to an instance of an object."

        public DangerZonePushpin()
        {
            this.Style = Application.Current.Resources["DangerZonePushpinStyle"] as Style;
            this.MouseEnter += new MouseEventHandler(DangerZonePushpin_MouseEnter);
            this.MouseLeave += new MouseEventHandler(DangerZonePushpin_MouseLeave);
        }
 
        void DangerZonePushpin_MouseLeave(object sender, MouseEventArgs e)
        {
            (this.FindName("animMouseLeave") as Storyboard).Begin();
        }
 
        void DangerZonePushpin_MouseEnter(object sender, MouseEventArgs e)
        {
            (this.FindName("animMouseEnter") as Storyboard).Begin();
        }

Open in new window

0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 23593261
well I learnt from your code only that,
u should try using
(Application.Current.Resources.FindName ("animMouseLeave") as Storyboard).Begin();
 
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 23593274
btw, better use of this style is to define in seperate resource dictionary and then merge it whereever it is required.
0
 
LVL 4

Author Comment

by:zstapic
ID: 23593854
Application.Current.Resources does not implement FindName method. How do I define it in separate resource dictionary and the merge it?
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 23597284
well, Application.current returns application class only.
so, Application.Resources  returns ResourceDictionary object.
ResourceDictionary implements FindName method.
which version of framework you are using? or which version of VS u r using?
0
 
LVL 4

Author Comment

by:zstapic
ID: 23598881
I am using .NET 3.5 sp1 with VS 2008 sp installed.
0
 
LVL 8

Accepted Solution

by:
unmeshdave earned 1500 total points
ID: 23603547

well your entire style you can define in an xaml within tags
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

<Style>..........</Style>
<Storyboard>....</Storyboard>
</ResourceDictionary>

Now, In your xaml where you want to merge,
In Resources section, windows.resources or Application.resources,
<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="related path of above xaml" />
        </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
 
 
0
 
LVL 4

Author Comment

by:zstapic
ID: 23610163
The solution was to use GetTemplateChild method. Thanks
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question