2016-07-25 58 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类来初始化所有屏幕宽度/高度值。