2016-07-25 56 views
1

我在申請時,載有下面的代碼窗口動畫滑動:窗口動畫,如果窗口狀態是最大化

<Window.Triggers> 
    <EventTrigger RoutedEvent="Window.Loaded"> 
     <EventTrigger.Actions> 
      <BeginStoryboard> 
       <Storyboard BeginTime="0" Duration="0:0:1"> 

        <DoubleAnimation Storyboard.TargetName="parent" Storyboard.TargetProperty="(Window.Left)" From="1920" To="0" AutoReverse="true" BeginTime="0:0:0" Duration="0:0:1" /> 
       </Storyboard> 
      </BeginStoryboard> 


     </EventTrigger.Actions> 
    </EventTrigger> 
</Window.Triggers> 

它工作正常,但是當我使的WindowState =「最大化」主窗口,動畫不起作用。

回答

1

這是Windows中的限制,而不是WPF--當它最大化時,無法更改窗口的位置。動畫實際上運行(Left值更改),但它沒有效果。

你可以做的是動畫的窗口,同時它在Normal狀態,一旦動畫完成最大化它:

<Window x:Class="WpfApp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:app="clr-namespace:WpfApp" 
     Name="parent" 
     WindowState="Normal" 
     Top="0" 
     Left="{x:Static SystemParameters.FullPrimaryScreenWidth}" 
     Width="{x:Static SystemParameters.FullPrimaryScreenWidth}" 
     Height="{x:Static SystemParameters.FullPrimaryScreenHeight}" 
     d:DataContext="{d:DesignData ViewModel}"> 
    <Window.Triggers> 
     <EventTrigger RoutedEvent="Window.Loaded"> 
      <EventTrigger.Actions> 
       <BeginStoryboard> 
        <Storyboard> 
         <DoubleAnimation Storyboard.TargetName="parent" 
             Storyboard.TargetProperty="(Window.Left)" 
             To="0" 
             Duration="0:0:1" /> 
         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="parent" 
                 Storyboard.TargetProperty="WindowState"> 
          <DiscreteObjectKeyFrame Value="{x:Static WindowState.Maximized}" 
                KeyTime="0:0:1" /> 
         </ObjectAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger.Actions> 
     </EventTrigger> 
    </Window.Triggers> 

注意此代碼將只針對正常系統的一臺顯示器工作。否則,您必須使用Windows Forms Screen類來初始化所有屏幕寬度/高度值。