none
Silverlight Canvas DoubleAnimation Canvas.GetLeft RRS feed

  • 问题

  • Canvas 布局,里面放置了一个UserControl,动画 控制usercontrol的位置

    a->b正常,b->a的时候,为什么Canvas.GetLeft(this)一直是0?

    public partial class SlTestControl : UserControl
        {
            #region 动画资源
            private Storyboard PanelAnimation;
            private DoubleAnimation AnimationX;
            #endregion
    
            public SlTestControl()
            {
                InitializeComponent();
                InitInternalParam();
            }
    
            //初始化内部数据
            private void InitInternalParam()
            {
                AnimationX = new DoubleAnimation();
                AnimationX.Duration = new Duration(TimeSpan.FromSeconds(0.6));
                AnimationX.From = 0;
                AnimationX.To = 0;
                Storyboard.SetTarget(AnimationX, this);
                Storyboard.SetTargetProperty(AnimationX, new PropertyPath("(Canvas.Left)"));
    
                PanelAnimation = new Storyboard();
                PanelAnimation.FillBehavior = FillBehavior.HoldEnd;
                PanelAnimation.Children.Add(AnimationX);
            }
    
            //动画移动位置
            public void SetPanelPositionTo(double toX, double toY)
            {
                PanelAnimation.Stop();
                PanelAnimation.Children.Clear();
                PanelAnimation.Children.Add(AnimationX);
                AnimationX.From = Canvas.GetLeft(this);
                AnimationX.To = toX;
                PanelAnimation.Begin();
            }
        }

    <UserControl x:Class="SilverlightDoubleAnimationTest.SlTestControl"
        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"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        
        <Grid x:Name="LayoutRoot" Background="Blue">
    
        </Grid>
    </UserControl>
    <UserControl x:Class="SilverlightDoubleAnimationTest.MainPage"
        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"
        mc:Ignorable="d"
                 xmlns:local="clr-namespace:SilverlightDoubleAnimationTest"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Canvas x:Name="LayoutRoot" Background="White">
    
            <local:SlTestControl 
                                 Width="100"
                x:Name="Tc"
                                 Height="100" />
            <Button 
                    Width="100"
                    Height="40"
                    Content="Move"
                    Canvas.Left="0"
                    Canvas.Top="199"
                    Click="Button_Click" />
        </Canvas>
    </UserControl>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SilverlightDoubleAnimationTest
    {
        public partial class MainPage : UserControl
        {
            bool BoolTest = true;
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                if(BoolTest)
                {
                    Tc.SetPanelPositionTo(300, 0);
                }
                else
                {
                    Tc.SetPanelPositionTo(0, 0);
                }
                BoolTest = !BoolTest;
            }
        }
    }
    

    等待热心人!

    2014年2月13日 9:17

答案

  • Hi, StoryBoard.Stop之后,已经进行动画处理的属性会恢复原值,所以在Stop后再调用Canvas.GetLeft就会返回原值0,可以稍作修改就能看到正常的效果:

            public void SetPanelPositionTo(double toX, double toY)
            {
                double left = Canvas.GetLeft(this);
                PanelAnimation.Stop();
                PanelAnimation.Children.Clear();
                PanelAnimation.Children.Add(AnimationX);
                AnimationX.From = left;
                AnimationX.To = toX;
                PanelAnimation.Begin();
            }

    • 已标记为答案 SZKK 2014年2月18日 4:03
    2014年2月13日 10:25