none
StoryBoard.Stop方法为什么无反映 RRS feed

  • 问题

  • <Window x:Class="wpfShopping.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="650" Width="398" Loaded="Window_Loaded" MouseLeftButtonDown="Window_MouseLeftButtonDown">
        <Canvas>

            <StackPanel Canvas.Top="0" x:Name="sp1" Orientation="Horizontal">
                <StackPanel.RenderTransform>
                    <TransformGroup>
                        <TranslateTransform X="0" Y="0"/>
                    </TransformGroup>
                </StackPanel.RenderTransform>
                <Image Source="icons/1.ico"></Image>
                <Image Source="icons/2.ico"></Image>
                <Image Source="icons/4.ico"></Image>
            </StackPanel>
            <StackPanel Canvas.Top="125" x:Name="sp2" Orientation="Horizontal">
                <StackPanel.RenderTransform>
                    <TransformGroup>
                        <TranslateTransform X="0" Y="0"/>
                    </TransformGroup>
                </StackPanel.RenderTransform>
                <Image Source="icons/52.ico"></Image>
                <Image Source="icons/53.ico"></Image>
                <Image Source="icons/54.ico"></Image>
            </StackPanel>
            <StackPanel Canvas.Top="250" x:Name="sp3" Orientation="Horizontal">
                <StackPanel.RenderTransform>
                    <TransformGroup>
                        <TranslateTransform X="0" Y="0"/>
                    </TransformGroup>
                </StackPanel.RenderTransform>
                <Image Source="icons/11.ico"></Image>
                <Image Source="icons/12.ico"></Image>
                <Image Source="icons/13.ico"></Image>
            </StackPanel>
            <StackPanel Canvas.Top="375" x:Name="sp4" Orientation="Horizontal">
                <StackPanel.RenderTransform>
                    <TransformGroup>
                        <TranslateTransform X="0" Y="0"/>
                    </TransformGroup>
                </StackPanel.RenderTransform>
                <Image Source="icons/s1.ico"></Image>
                <Image Source="icons/s2.ico"></Image>
                <Image Source="icons/s3.ico"></Image>
            </StackPanel>
            <Button x:Name="btntest" Content="stop" Click="btntest_Click"></Button>
        </Canvas>
    </Window>
    cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Media.Animation;


    namespace wpfShopping
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            Storyboard sb = new Storyboard();
            public MainWindow()
            {
                InitializeComponent();
                NameScope.SetNameScope(this, new NameScope());
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {

            }

            private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {

                StackPanel sp = (e.Source as Image).Parent as StackPanel;
                CreateAnimation(sp);

            }
            private void CreateAnimation(StackPanel sp)
            {


                DoubleAnimation da = new DoubleAnimation();
                da.From = 0; da.To = 200;
                da.Duration = TimeSpan.FromSeconds(5);


                Storyboard sb = new Storyboard();
                //sb.RepeatBehavior = RepeatBehavior.Forever;
                //sb.AutoReverse = true;
                sb.Children.Add(da);


                this.RegisterName(sp.Name, sp);
                Storyboard.SetTargetName(da, sp.Name);
                Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));

                try
                {

                    sb.Begin(this, true);
                    // sb.GetCurrentState(sp1);
                    // sb.Stop(sp1);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }

            }

            void sb_CurrentTimeInvalidated(object sender, EventArgs e)
            {
                // string str = sender.ToString();

            }

            private void btntest_Click(object sender, RoutedEventArgs e)
            {
                sb.Stop(this);
            }
        }
    }
    点击Stop为什么无反应啊,请教哥们姐们!

    2011年1月20日 7:59

答案

  • 你好 偷着想你,

    根据你的代码,我已经重现了你所说的问题:

    ·         -->点击Stop为什么无反应啊,请教哥们姐们!

    你之所以不能停是因为你在“CreateAnimation”的时候新建了一个“Storyboard”的对象(临时对象),然后当你点击Button的时候,并没有停掉你新建的“Storyboard”对象,所以你的“ sb.Stop(this);” 不会工作。

    你删掉“CreateAnimation”函数里面的“Storyboard”对象, 你的问题就会解决。

     

    Best regards,

    Sheldon _Xiao


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月20日 10:12
    版主