When I try to animate my main window only Height is getting animated, however when i use the same code to animate my grid it is working fine.Please help me out, as i am new to wpf
<Window x:Class="TestingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestingDemo"
mc:Ignorable="d"
Name="myWindow"
AllowsTransparency="True"
WindowStyle="None"
Background="Blue"
Title="MainWindow" Height="100" Width="100">
<Grid>
<TextBlock MouseDown="TextBlock_MouseDown">
OpenMe
<TextBlock.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="myWindow"
Storyboard.TargetProperty="Height"
From="100"
To="600"></DoubleAnimation>
<DoubleAnimation
Storyboard.TargetName="myWindow"
Storyboard.TargetProperty="Width"
From="100"
To="600"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>
</Window>
W
It's a pity that changing the Width
and Height
of Window
is separated and expensive. The Width
and Height
of Window
are WPF dependency property but they work in a native way. The same properties of Grid
are working in WPF way.
It's recommended to avoid animating the Width
and Height
property of Window
. Instead, animating the inner framework elements.
I've tried this code below and it worked unsmoothly:
while (true)
{
await Task.Delay(10);
Width += 10;
Height += 10;
}
Here is what i am doing for the same and it works like a charm:
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Width" From="0" To="yourWidth" Duration="0:0:1" FillBehavior="HoldEnd" AutoReverse="False" />
<DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="yourHeight" Duration="0:0:3" FillBehavior="HoldEnd" AutoReverse="False"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
I have hooked it for Window Loaded event but you can change it as you wish but the key is If you want smooth animation then you have to manually push frames in a timer or thread like:
/// <summary>
///
/// </summary>
public static class DispatcherHelper
{
/// <summary>
/// Simulate Application.DoEvents function of <see cref=" System.Windows.Forms.Application"/> class.
/// </summary>
[SecurityPermissionAttribute ( SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode )]
public static void DoEvents ( )
{
DispatcherFrame frame = new DispatcherFrame ( );
Dispatcher.CurrentDispatcher.BeginInvoke ( DispatcherPriority.Background,
new DispatcherOperationCallback ( ExitFrames ), frame );
try
{
Dispatcher.PushFrame ( frame );
}
catch ( InvalidOperationException )
{
}
}
/// <summary>
///
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
private static object ExitFrames ( object frame )
{
( ( DispatcherFrame ) frame ).Continue = false;
return null;
}
}
Please be careful as it is not advised to push frames manually but if you know what you're doing then knock yourself out! :).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.