2 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個贊
遺憾的是,更改Width和Height的Window分離且昂貴。該Width和Height的Window是WPF依賴屬性,但他們在本機(jī)的方式工作。的相同屬性Grid以 WPF 方式工作。
建議避免為 的Width和Height屬性設(shè)置動畫Window。相反,動畫內(nèi)部框架元素。
我已經(jīng)嘗試過下面的代碼,但它運(yùn)行不流暢:
while (true)
{
await Task.Delay(10);
Width += 10;
Height += 10;
}

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超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;
}
}
請小心,因?yàn)椴唤ㄗh手動推送幀,但如果您知道自己在做什么,那么請自重!:)。
- 2 回答
- 0 關(guān)注
- 167 瀏覽
添加回答
舉報