猿问

WPF 双动画无法正常工作 C#

当我尝试为我的主窗口设置动画时,只有 Height 正在设置动画,但是当我使用相同的代码为我的网格设置动画时,它工作正常。请帮助我,因为我是 wpf 的新手


<Window x:Class="TestingDemo.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:local="clr-namespace:TestingDemo"

    mc:Ignorable="d"

    Name="myWindow"

    AllowsTransparency="True"

    WindowStyle="None"

    Background="Blue"

    Title="MainWindow" Height="100" Width="100">

<Grid>

    <TextBlock MouseDown="TextBlock_MouseDown">

        OpenMe

        <TextBlock.Triggers>

            <EventTrigger RoutedEvent="MouseDown">

                <BeginStoryboard>

                    <Storyboard>

                        <DoubleAnimation

                            Storyboard.TargetName="myWindow"

                            Storyboard.TargetProperty="Height"

                            From="100"

                            To="600"></DoubleAnimation>

                        <DoubleAnimation

                            Storyboard.TargetName="myWindow"

                            Storyboard.TargetProperty="Width"

                            From="100"

                            To="600"></DoubleAnimation>



                    </Storyboard>

                </BeginStoryboard>

            </EventTrigger>

        </TextBlock.Triggers>

    </TextBlock>

   </Grid>

</Window>


BIG阳
浏览 153回答 2
2回答

慕田峪9158850

遗憾的是,更改Width和Height的Window分离且昂贵。该Width和Height的Window是WPF依赖属性,但他们在本机的方式工作。的相同属性Grid以 WPF 方式工作。建议避免为 的Width和Height属性设置动画Window。相反,动画内部框架元素。我已经尝试过下面的代码,但它运行不流畅:while (true){&nbsp; &nbsp; await Task.Delay(10);&nbsp; &nbsp; Width += 10;&nbsp; &nbsp; Height += 10;}

慕运维8079593

这是我正在做的事情,它就像一个魅力:&nbsp; <Window.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; <EventTrigger RoutedEvent="Loaded">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <BeginStoryboard>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Storyboard>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DoubleAnimation Storyboard.TargetProperty="Width" From="0" To="yourWidth" Duration="0:0:1" FillBehavior="HoldEnd" AutoReverse="False" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="yourHeight" Duration="0:0:3" FillBehavior="HoldEnd" AutoReverse="False"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Storyboard>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </BeginStoryboard>&nbsp; &nbsp; &nbsp; &nbsp; </EventTrigger>&nbsp;</Window.Triggers>&nbsp; &nbsp; 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:&nbsp; /// <summary>&nbsp; &nbsp; ///&nbsp;&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public static class DispatcherHelper&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// Simulate Application.DoEvents function of <see cref=" System.Windows.Forms.Application"/> class.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; [SecurityPermissionAttribute ( SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode )]&nbsp; &nbsp; &nbsp; &nbsp; public static void DoEvents ( )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DispatcherFrame frame = new DispatcherFrame ( );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dispatcher.CurrentDispatcher.BeginInvoke ( DispatcherPriority.Background,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DispatcherOperationCallback ( ExitFrames ), frame );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dispatcher.PushFrame ( frame );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch ( InvalidOperationException )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; ///&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; /// <param name="f"></param>&nbsp; &nbsp; &nbsp; &nbsp; /// <returns></returns>&nbsp; &nbsp; &nbsp; &nbsp; private static object ExitFrames ( object frame )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( ( DispatcherFrame ) frame ).Continue = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }请小心,因为不建议手动推送帧,但如果您知道自己在做什么,那么请自重!:)。
随时随地看视频慕课网APP
我要回答