Find storyboard object to bind events
[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:
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.
How would I do this for Storyboards? I tried:
Code: Select all
DependencyObject* object = uiRenderer->GetXAML()->FindName<DependencyObject>("LogoStoryBoard");
Code: Select all
<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>
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: Find storyboard object to bind events
Objects that are defined inside the Resources property can be obtained by calling FindResource.
In your sample you will do:
In your sample you will do:
Code: Select all
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
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.
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.
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: Find storyboard object to bind events
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:
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:
Code: Select all
<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
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], Semrush [Bot] and 4 guests