Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中设置边距属性的动画_C#_Wpf_Xaml_Animation_Margin - Fatal编程技术网

C# 如何在WPF中设置边距属性的动画

C# 如何在WPF中设置边距属性的动画,c#,wpf,xaml,animation,margin,C#,Wpf,Xaml,Animation,Margin,我想移动一个矩形对象,使其在x轴上移动。我是WPF动画新手,从以下内容开始: <Storyboard x:Key="MoveMe"> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="GroupTileSecond" Storybo

我想移动一个矩形对象,使其在x轴上移动。我是WPF动画新手,从以下内容开始:

<Storyboard x:Key="MoveMe">
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
                                   Storyboard.TargetName="GroupTileSecond"
                                   Storyboard.TargetProperty="(**Margin.Left**)">

        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="**134, 70,0,0**" />
        <SplineDoubleKeyFrame KeyTime="00:00:03" Value="**50, 70,0,0**" />
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

显然,我发现我不能将
Margin.Left
用作
Storyboard.TargetProperty
或在Value属性中使用
134,70,0,0


那么,如何在XAML WPF中移动对象。

您不能设置Margin.Left动画(因为
Left
不是依赖属性),但可以设置
Margin
动画。使用:


134,70,0,0
50,70,0,0
有一些备选方案允许您使用
双动画
,而不是关键帧:

  • 将目标放置在画布中,并使用
    Canvas.Left
    设置其x位置的动画
  • 将a应用于目标,并使用
    TranslateTransform.x
    设置其x位置的动画
    作为另一种回答,您可以使用动画进行
    水平对齐
    垂直对齐
    属性

    例如:

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GroupTileSecond" 
                                   Storyboard.TargetProperty="HorizontalAlignment">
    
        <DiscreteObjectKeyFrame KeyTime="0:0:0">
            <DiscreteObjectKeyFrame.Value>
                <HorizontalAlignment>Center</HorizontalAlignment>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
    
    
    居中
    
    事实上,你可以做你想做的事情,就像你想做的那样,使用
    渲染转换
    混合一些
    双动画
    ,甚至添加一些额外的天赋,例如

    <Grid x:Name="TheObject" Opacity="0">
        <Grid.RenderTransform>
            <TranslateTransform x:Name="MoveMeBaby" X="50" />
        </Grid.RenderTransform>
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="MoveMeBaby" 
                                                       Storyboard.TargetProperty="X">
                            <SplineDoubleKeyFrame KeyTime="0:0:1.25" Value="0" />
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="TheObject"
                                                       Storyboard.TargetProperty="Opacity">
                            <SplineDoubleKeyFrame KeyTime="0:0:1.55" Value="1" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Grid.Triggers>
    </Grid>
    
    
    

    将在X轴上移动该对象50px,甚至在移动过程中淡入淡出。给它一个快照,并使用
    X
    属性和
    KeyTime
    的值来获得您想要的。希望这有帮助,干杯。

    Margin
    属性可以使用
    ThicknessAnimation

    <Storyboard >
         <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
            <SplineThicknessKeyFrame KeyTime="00:00:00" Value="134, 70,0,0" />
            <SplineThicknessKeyFrame KeyTime="00:00:03" Value="50, 70,0,0" />
         </ThicknessAnimationUsingKeyFrames>
    </Storyboard>
    

    我刚刚创建了一个动画。暗号

    using System;
    using System.Windows;
    using System.Windows.Media.Animation;
    
    namespace ImagesSwitcher
    {
        class MarginAnimation : AnimationTimeline
        {
            protected override Freezable CreateInstanceCore()
            {
                return new MarginAnimation();
            }
    
        public override Type TargetPropertyType => typeof(Thickness);
    
        private double GetContrast(double dTo,double dFrom,double process)
        {
            if (dTo < dFrom)
            {
                return dTo + (1 - process) * (dFrom - dTo);
            }
    
            return dFrom + (dTo - dFrom) * process;
        }
    
        public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
        {
            if (!From.HasValue || !To.HasValue || animationClock.CurrentProgress == null) return null;
            var progress = animationClock.CurrentProgress.Value;
    
            if (progress.Equals(0)) return null;
    
            if (progress.Equals(1)) return To.Value; 
    
            var fromValue = From.Value;
            var toValue = To.Value;
    
            var l = GetContrast(toValue.Left ,fromValue.Left, progress);
            var t = GetContrast(toValue.Top, fromValue.Top, progress);
            var r = GetContrast(toValue.Right, fromValue.Right, progress);
            var b = GetContrast(toValue.Bottom, fromValue.Bottom, progress);
    
            return new Thickness(l,t,r,b);
        }
    
        public Thickness? To
        {
            set => SetValue(ToProperty, value);
            get => (Thickness)GetValue(ToProperty);
        }
        public static DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(Thickness), typeof(MarginAnimation), new PropertyMetadata(null));
    
        public Thickness? From
        {
            set => SetValue(FromProperty, value);
            get => (Thickness)GetValue(FromProperty);
        }
        public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(Thickness), typeof(MarginAnimation), new PropertyMetadata(null));
    
    }
    
    使用系统;
    使用System.Windows;
    使用System.Windows.Media.Animation;
    名称空间映像开关
    {
    类边缘动画:AnimationTimeline
    {
    受保护的重写Freezable CreateInstanceCore()
    {
    返回新的MarginAnimation();
    }
    公共覆盖类型TargetPropertyType=>typeof(厚度);
    专用双对比度(双dTo、双dFrom、双进程)
    {
    如果(dToSetValue(ToProperty,value);
    get=>(厚度)GetValue(TopProperty);
    }
    public static dependencProperty ToProperty=dependencProperty.Register(“To”、typeof(Thickness)、typeof(MarginAnimation)、new PropertyMetadata(null));
    公共厚度?来自
    {
    set=>SetValue(FromProperty,value);
    get=>(厚度)GetValue(FromProperty);
    }
    public static dependencProperty FromProperty=dependencProperty.Register(“From”、typeof(Thickness)、typeof(MarginAnimation)、new PropertyMetadata(null));
    }
    

    }

    @McGarnagle&AnatoliyNikolaev-+1也祝你们俩,当三个家伙能想出不同的方法拧入一个灯泡时,你们一定会喜欢的:)哈哈!另一个+1,忘了厚度,我只是觉得你把问题摆在一些人面前,每个人都有不同的聪明方法。光是这一点就太棒了,对于OP的第一篇文章来说,这是一个很棒的节目,我喜欢这样有趣的节目。干杯工作得很有魅力。谢谢我收到以下错误异常“{”无法使用“System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames”对“System.Windows.Shapes.Rectangle”上的“Margin”属性设置动画。有关详细信息,请参阅内部异常。“}”内部异常“{”动画应用于“Margin”属性,计算“134,70,0,0”的当前值,该值不是该属性的有效值。“}”@gajakannahm。。。尝试显式指定厚度,如上面我的编辑中所示。
    using System;
    using System.Windows;
    using System.Windows.Media.Animation;
    
    namespace ImagesSwitcher
    {
        class MarginAnimation : AnimationTimeline
        {
            protected override Freezable CreateInstanceCore()
            {
                return new MarginAnimation();
            }
    
        public override Type TargetPropertyType => typeof(Thickness);
    
        private double GetContrast(double dTo,double dFrom,double process)
        {
            if (dTo < dFrom)
            {
                return dTo + (1 - process) * (dFrom - dTo);
            }
    
            return dFrom + (dTo - dFrom) * process;
        }
    
        public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
        {
            if (!From.HasValue || !To.HasValue || animationClock.CurrentProgress == null) return null;
            var progress = animationClock.CurrentProgress.Value;
    
            if (progress.Equals(0)) return null;
    
            if (progress.Equals(1)) return To.Value; 
    
            var fromValue = From.Value;
            var toValue = To.Value;
    
            var l = GetContrast(toValue.Left ,fromValue.Left, progress);
            var t = GetContrast(toValue.Top, fromValue.Top, progress);
            var r = GetContrast(toValue.Right, fromValue.Right, progress);
            var b = GetContrast(toValue.Bottom, fromValue.Bottom, progress);
    
            return new Thickness(l,t,r,b);
        }
    
        public Thickness? To
        {
            set => SetValue(ToProperty, value);
            get => (Thickness)GetValue(ToProperty);
        }
        public static DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(Thickness), typeof(MarginAnimation), new PropertyMetadata(null));
    
        public Thickness? From
        {
            set => SetValue(FromProperty, value);
            get => (Thickness)GetValue(FromProperty);
        }
        public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(Thickness), typeof(MarginAnimation), new PropertyMetadata(null));
    
    }