繁体   English   中英

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

[英]WPF Double Animation Not working Properly C#

当我尝试动画我的主窗口时,只有高度变得动画,但是当我使用相同的代码来动画我的网格它工作正常。请帮助我,因为我是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>

w ^

遗憾的是,改变WindowWidthHeight是分开且昂贵的。 WindowWidthHeight是WPF依赖属性,但它们以本机方式工作。 Grid的相同属性以WPF方式工作。

建议避免设置WindowWidthHeight属性的动画。 相反,动画内部框架元素。

我在下面尝试过这段代码并且工作不顺利:

while (true)
{
    await Task.Delay(10);
    Width += 10;
    Height += 10;
}

这就是我正在为此做的事情,它就像一个魅力:

  <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;
        }
    }

请小心,因为不建议手动推框,但如果你知道你正在做什么,那就自己敲门吧! :)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM