如果窗口状态最大化,则加载窗口动画

时间:2016-07-25 10:54:39

标签: .net wpf

当窗口加载以下代码时,我正在应用滑动动画:

<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 =“Maximized”时,动画不起作用。

1 个答案:

答案 0 :(得分: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窗体Screen类初始化所有屏幕宽度/高度值。