none
请教有关StoryBoard的问题 RRS feed

  • 问题

  • 我在窗体中做了一个loading的图标:

            <Path Name="LoadingIcon2" Stroke="#888" Visibility="Collapsed" StrokeThickness="3" Width="24" Height="24" RenderTransformOrigin="0.5,0.5" Panel.ZIndex="100" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="6 0 0 6">
                <Path.RenderTransform>
                    <RotateTransform Angle="0"></RotateTransform>
                </Path.RenderTransform>
                <Path.Data>
                    <GeometryGroup>
                        <PathGeometry Figures="M 12,2 A 10,10 0 1 0 22,12"></PathGeometry>
                    </GeometryGroup>
                </Path.Data>
            </Path>
    

    然后通过代码去显示,并且让其旋转:

    private void ShowLoading() { LoadingIcon2.Visibility = Visibility.Visible; LoadingIcon2.Opacity = 1; DoubleAnimation ani = new DoubleAnimation(0, 360, TimeSpan.FromMilliseconds(1000)); Storyboard st = new Storyboard(); Storyboard.SetTarget(ani, LoadingIcon2); Storyboard.SetTargetProperty(ani, new PropertyPath("RenderTransform.Angle")); st.RepeatBehavior = RepeatBehavior.Forever; st.Children.Add(ani); st.Begin(); }

    private void CloseLoading() { DoubleAnimation ani = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(500)); ani.EasingFunction = new CubicEase(); Storyboard st = new Storyboard(); Storyboard.SetTarget(ani, LoadingIcon2); Storyboard.SetTargetProperty(ani, new PropertyPath("Opacity")); st.Children.Add(ani); st.Completed += (o, evt) => { LoadingIcon2.Visibility = Visibility.Collapsed; }; st.Begin(); }


    showLoading方法可以让那个图标产生旋转效果;

    然后调用CloseLoading方法,可以将图标淡出并最终隐藏起来;

    当我按照:showLoading - CloseLoading的顺序去调用的时候,产生的效果正如我预想中的那样;

    可是,当我Close了之后再Show的时候,却发现图标不显示了。我明明在Show方法那里强制将Opacity设置为1的,可是通过“实时可视化树”查看对象属性,却发现Opacity为零,这是为什么?


    da jia hao!

    2017年10月25日 6:46

答案


  • Hi liubin,

    >>可是,当我Close了之后再Show的时候,却发现图标不显示了。我明明在Show方法那里强制将Opacity设置为1的,可是通过“实时可视化树”查看对象属性,却发现Opacity为零,这是为什么?

    你需要重复使用动画之前,先将之前的动画效果清除。这样不会出现问题。

         private void ShowLoading()
            {
                LoadingIcon2.Opacity = 1;
                LoadingIcon2.Visibility = Visibility.Visible;
                LoadingIcon2.ApplyAnimationClock(Path.OpacityProperty, null); // remove animation
                DoubleAnimation ani = new DoubleAnimation(0, 360, TimeSpan.FromMilliseconds(1000));
                Storyboard st = new Storyboard();
                Storyboard.SetTarget(ani, LoadingIcon2);
                Storyboard.SetTargetProperty(ani, new PropertyPath("RenderTransform.Angle"));
                st.RepeatBehavior = RepeatBehavior.Forever;
                st.Children.Add(ani);
                st.Begin();
            }
    
            private void CloseLoading()
            {
                LoadingIcon2.ApplyAnimationClock(Path.RenderTransformProperty, null); // remove animation
                DoubleAnimation ani = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(500));
                ani.EasingFunction = new CubicEase();
                Storyboard st = new Storyboard();
                Storyboard.SetTarget(ani, LoadingIcon2);
                Storyboard.SetTargetProperty(ani, new PropertyPath("Opacity"));
                st.Children.Add(ani);
                st.Completed += (o, evt) =>
                {
                    LoadingIcon2.Visibility = Visibility.Collapsed;
                };
                st.Begin();
            }
    


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 liubin 2017年10月26日 3:03
    2017年10月26日 2:18
    版主

全部回复


  • Hi liubin,

    >>可是,当我Close了之后再Show的时候,却发现图标不显示了。我明明在Show方法那里强制将Opacity设置为1的,可是通过“实时可视化树”查看对象属性,却发现Opacity为零,这是为什么?

    你需要重复使用动画之前,先将之前的动画效果清除。这样不会出现问题。

         private void ShowLoading()
            {
                LoadingIcon2.Opacity = 1;
                LoadingIcon2.Visibility = Visibility.Visible;
                LoadingIcon2.ApplyAnimationClock(Path.OpacityProperty, null); // remove animation
                DoubleAnimation ani = new DoubleAnimation(0, 360, TimeSpan.FromMilliseconds(1000));
                Storyboard st = new Storyboard();
                Storyboard.SetTarget(ani, LoadingIcon2);
                Storyboard.SetTargetProperty(ani, new PropertyPath("RenderTransform.Angle"));
                st.RepeatBehavior = RepeatBehavior.Forever;
                st.Children.Add(ani);
                st.Begin();
            }
    
            private void CloseLoading()
            {
                LoadingIcon2.ApplyAnimationClock(Path.RenderTransformProperty, null); // remove animation
                DoubleAnimation ani = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(500));
                ani.EasingFunction = new CubicEase();
                Storyboard st = new Storyboard();
                Storyboard.SetTarget(ani, LoadingIcon2);
                Storyboard.SetTargetProperty(ani, new PropertyPath("Opacity"));
                st.Children.Add(ani);
                st.Completed += (o, evt) =>
                {
                    LoadingIcon2.Visibility = Visibility.Collapsed;
                };
                st.Begin();
            }
    


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 liubin 2017年10月26日 3:03
    2017年10月26日 2:18
    版主
  • 太谢谢了!这个东西,自学的话真的不知道怎么做。有微软这个论坛的大佬指导一下,效率可高了。

    da jia hao!

    2017年10月26日 3:03