Rick
Topic Author
Posts: 50
Joined: 26 Nov 2013, 15:35

Find storyboard object to bind events

17 Apr 2015, 02:49

[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.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Find storyboard object to bind events

17 Apr 2015, 19:51

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); 
 
Rick
Topic Author
Posts: 50
Joined: 26 Nov 2013, 15:35

Re: Find storyboard object to bind events

17 Apr 2015, 20:26

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.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Find storyboard object to bind events

17 Apr 2015, 20:55

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> 
 
Rick
Topic Author
Posts: 50
Joined: 26 Nov 2013, 15:35

Re: Find storyboard object to bind events

17 Apr 2015, 21:01

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.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 30 guests