WPF Animation not starting for the first time

I have a window that should disappear on display. At the first use, the animation does not start. Subsequent use, it works great. This is a query that I create to show something - I do not support it and reuse instances. I instantiate the request, set various properties such as .Top and .Left, and then call ShowDialog. In the constructor, I set the DataContext. If I comment on this (since another thread suggested that this might be caused by setting the data context during the animation playback), then it gets rid of the problem. However, I do not call ShowDialog until the constructor is called. And since I create an instance of a new instance every time I want to use it, I don’t understand why it is only the first time that there is a problem.

It is as if the assembly is loading for the first time, but the VS output window does not show this.

The window starts with zero opacity, therefore, becoming visible, it shows that the animation starts, but just does not have time to start. If I set BeginTime to half a second, it will work every time, but obviously it will be slower when it appears in response to a button press and may still be insufficient on slower machines.

I define animation in XAML with

<Window.Triggers> <EventTrigger RoutedEvent="Window.Loaded"> <BeginStoryboard> <Storyboard Name="FormFade"> <DoubleAnimation Name="FormFadeAnimation" Storyboard.TargetProperty="(Window.Opacity)" From="0.0" To="1.0" Duration="0:0:0.25" AutoReverse="False" RepeatBehavior="1x" BeginTime="0:0:0"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers> 

Is there a more appropriate event to include? I tried Window.Initialized and Window.IsVisibleChanged, but none of them were redirected, so they throw an exception when parsing XAML.

I could shift everything to the code behind (since in any case it is necessary to make it disappear), but prefer that the material be in xaml if it can.

I could assign a DataContext from Dispatcher.BeginInvoke, but it seems messy and where in the future will be filled in the fields that will probably happen after the animation and look bad, so I will just hide the problem until later.

Does anyone have any thoughts on this?

+2
source share
1 answer

try this code:

 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication2.MainWindow" x:Name="thisWindow" Title="MainWindow" Width="640" Height="480" AllowsTransparency="True" WindowStyle="None"> <Window.Resources> <Storyboard x:Key="MyStoryboard"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="thisWindow"> <EasingDoubleKeyFrame KeyTime="0" Value="0"/> <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="1"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard Storyboard="{StaticResource MyStoryboard}"/> </EventTrigger> </Window.Triggers> <Grid x:Name="LayoutRoot"/> </Window> 
0
source

Source: https://habr.com/ru/post/1492862/


All Articles