tags:

views:

40

answers:

2

I'm trying to kick off an animation at the click of a button. The following code looks reasonable, but is throwing a runtime error. Ideally I'd like to just point the storyboard to a command. Is that possible (I Googled and found nothing to indicate that it is)

                <Button Width="50" Height="24" Content="X">
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <BeginStoryboard Storyboard="{StaticResource ShowTemplateSelector}">
                            </BeginStoryboard>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>

Here's the storyboard:

<UserControl.Resources>
    <Storyboard x:Name="ShowTemplateSelector">
        <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Width" From="0" To="332" Duration="0:0:0.4" />
        <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Height" From="0" To="100" Duration="0:0:0.4" />
    </Storyboard>

The runtime error says:

Microsoft JScript runtime error: Unhandled Error in Silverlight Application 2531 An error has occurred. [Line: 97 Position: 55] at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at SilverlightApplication8.MainPage.InitializeComponent() at SilverlightApplication8.MainPage..ctor() at SilverlightApplication8.App.Application_Startup(Object sender, StartupEventArgs e) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Line 97 is this:

<EventTrigger RoutedEvent="Button.Click">
A: 

Change this...

<BeginStoryboard Storyboard="{StaticResource ShowTemplateSelector}">
</BeginStoryboard>

to this...

<BeginStoryboard Storyboard="{DynamicResource ShowTemplateSelector}">
</BeginStoryboard>
Aaron
DynamicResource markup extension doesn't exist in Silverlight. It is only in WPF.
Stephan
@Stephan thanks for the info...just looked it up and it said if you are moving over from WPF to SL to move to a StaticResource and reference it that way...which is what he is doing...hmm...
Aaron
Believe me when I say, I wish this was the solution. I was really hoping MS would get this added in SL 4.
Stephan
There seems to be a lot that hasn't been added to SL 4.... "it doesn't work in SL" seems to be a common theme :(
Adam
@Adam wrote an LOB application in SL 2...believe me SL 4 looks fantastic in comparison...
Aaron
I've always been fascinated by the idea of writing LOB in SL. Why not just use WPF with ClickOnce? Sure, CO isn't the best thing in the world, but it has be preferable to SL...
Adam
Our reason was we wanted to run it on varying OS'es and the fact that is was thin, run in a browser which is nice from a large enterprise stance in terms of what they allow on their machines, etc.. I must say that as of SL 4 an LOB app would not be a problem. The problem surfaces when you know you can do X,Y,Z within WPF and you can't in SL.
Aaron
+1  A: 

You need to move your storyboard into the trigger. If you try to reference it as a StaticResource it is unable to resolve the names in the naming scope of the ResourceDictionary

<EventTrigger RoutedEvent="Button.Click">
  <BeginStoryboard>
    <BeginStoryboard.Storyboard>
      <Storyboard x:Name="ShowTemplateSelector">
        <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Width" From="0" To="332" Duration="0:0:0.4" />
        <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Height" From="0" To="100" Duration="0:0:0.4" />
      </Storyboard>
  </BeginStoryboard.Storyboard>
</BeginStoryboard>
Stephan
Thank you. Is there *any* way to get the button to reference a storyboard in the resources section? I mean, other than ShowTemplateSelector.Begin(); in a click handler, of course.
Adam
Also, is this a limitation of Silverlight, or would WPF cause this same error too?
Adam
My explanation is a little poorly worded. The problem isn't it finding the storyboard. The StaticResource markup extension will fine the storyboard fine. The problem is that `Storyboard.TargetName="canvasStyleSelected"` can not resolve the name canvasStyleSelected because it isn't in scope. DynamicResource in WPF would work because it will lookup the names from the scope of the execution, but unfortunately that extension doesn't exist in SL. This is the only way I've found to do what you are describing.
Stephan