2 回答
TA贡献1794条经验 获得超7个赞
遗憾的是,更改Width和Height的Window分离且昂贵。该Width和Height的Window是WPF依赖属性,但他们在本机的方式工作。的相同属性Grid以 WPF 方式工作。
建议避免为 的Width和Height属性设置动画Window。相反,动画内部框架元素。
我已经尝试过下面的代码,但它运行不流畅:
while (true)
{
await Task.Delay(10);
Width += 10;
Height += 10;
}
TA贡献1876条经验 获得超5个赞
这是我正在做的事情,它就像一个魅力:
<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;
}
}
请小心,因为不建议手动推送帧,但如果您知道自己在做什么,那么请自重!:)。
- 2 回答
- 0 关注
- 152 浏览
添加回答
举报