Page 1 of 1

Find storyboard object to bind events

Posted: 17 Apr 2015, 02:49
by Rick
[Had this in another thread but decided to break it into it's own and deleted the question in the other thread.]

How would I do this for Storyboards? I tried:
DependencyObject* object = uiRenderer->GetXAML()->FindName<DependencyObject>("LogoStoryBoard");
<Grid x:Name="window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="800">
	<Grid.Resources>
		<Storyboard x:Key="LogoStoryBoard">

...

<Grid.Triggers>
		<EventTrigger RoutedEvent="FrameworkElement.Loaded">
			<BeginStoryboard Storyboard="{StaticResource LogoStoryBoard}"/>
		</EventTrigger>
	</Grid.Triggers>

But it came back null. I'm trying to link the Completed event via this dynamic way but can't get the control itself. The storyboard doesn't seem to be a child of UIElement so I tried the topmost parent they had.

Re: Find storyboard object to bind events

Posted: 17 Apr 2015, 19:51
by sfernandez
Objects that are defined inside the Resources property can be obtained by calling FindResource.

In your sample you will do:
FrameworkElement* window = uiRenderer->GetXAML()->FindName<FrameworkElement>("window");
Ptr<ResourceKeyString> key = ResourceKeyString::Create("LogoStoryboard");
Storyboard* logoStoryboard = window->FindResource<Storyboard>(key.GetPtr());
logoStoryboard->Completed() += MakeDelegate(this, &App::OnLogoStoryboardCompleted); 

Re: Find storyboard object to bind events

Posted: 17 Apr 2015, 20:26
by Rick
Thank you very much.

Maybe this is more of a general xaml question but what is the reason for this resource section? Does it need to exist? I was just following a tutorial on animation in blend and this is what the result was.

The reason I ask is because I'm looking at that generic way of handling events for Lua and this puts a wrinkle in that because the way I have to get this is via a different process than getting other controls like a button that isn't a resource. I just don't fully get this resource section and if it's required or why it exists in order to make a decision on the best way to handle something like this in my quest to make event handling more generic. Any insights you could share would be great.

Re: Find storyboard object to bind events

Posted: 17 Apr 2015, 20:55
by sfernandez
In a XAML you define a Logical Tree of UI elements and controls. Each of the elements can define a table of resources that can be used by the element and all its children elements.

Resources should be considered objects that can be reused across your xaml, and usually are classes deriving from Freezable, like Brushes, Geometries, Transforms, Animations... But also for defining helper objects like Converters or ViewModels. Other important kind of objects that are defined as resources are the Styles and Templates that are applied later to the controls.

Using resources has some important advantages: sharing resources thus increasing performance, and provide a centralized way to skin your interface.

For example:
<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel.Resources>
        <SolidColorBrush x:Key="defaultBg" Color="Silver"/>
        <ControlTemplate x:Key="btnTemplate" TargetType="Button">
            <Border Background="{StaticResource defaultBg}" CornerRadius="2">
                <ContentPresenter/>
            </Border>
        </ControlTemplate>
   </StackPanel.Resources>
   <Rectangle Widht="200" Height="10" Fill="{StaticResource defaultBg}" Margin="4"/>
   <Button Content="Ok" Template="{StaticResource btnTemplate}"/>
   <Button Content="Cancel" Template="{StaticResource btnTemplate}"/>
   <Rectangle Widht="200" Height="10" Fill="{StaticResource defaultBg}" Margin="4"/>
</StackPanel> 

Re: Find storyboard object to bind events

Posted: 17 Apr 2015, 21:01
by Rick
Ah OK, that make sense. So really in my situation there is no point to have my game logo animation as a resource because it will never be shared. I'll have to look at that and see if I can just move it out of the resource section so I don't have to use FindResource() function.