none
怎么在C#代码中实现PolyLine的虚线流动? RRS feed

  • 问题

  • 如题,谢谢。

    虚线已经搞定了,但一直没明白怎么将 PolyLine.StrokeDashOffset的偏移量挂载动画上。

    2014年9月28日 13:27

答案

  • 问题是:这个,我怎么在代码里实现呢?我不想要XAML的实现。

    XAML 和 C#代码之间的转换,不太会。试了试,没能移植过去。多谢!

    你好,

    >>这个,我怎么在代码里实现呢?我不想要XAML的实现

    以下是后台代码方式:

    private void CreateStoryboard(double fromvalue, double tovalue, TimeSpan duration)
    {
                DoubleAnimation animation = new DoubleAnimation(fromvalue, tovalue, duration);
                pl.BeginAnimation(Polyline.StrokeDashOffsetProperty, animation);
    
                Storyboard storyboard = new Storyboard();
                storyboard.Children.Add(animation);
                Storyboard.SetTarget(animation, pl);
    }
    
    private void pl_MouseEnter(object sender, MouseEventArgs e)
    {
                CreateStoryboard(0, 10, TimeSpan.FromSeconds(1));
    }

    XAML:

    <Canvas>
    <Polyline Name="pl" Points="100,50 10,50" Stroke="Black" StrokeThickness="4" StrokeDashArray="1, 1" StrokeDashOffset="0" MouseEnter="pl_MouseEnter" />
    </Canvas>


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月30日 7:59
    版主
  • 你好,

    >>1)但我不理解,为什么要加这句话:“Storyboard.SetTarget(animation, pl);”,我把这句话去掉,虚线也能流动。

    其实 pl.BeginAnimation(Polyline.StrokeDashOffsetProperty, animation); 就是开始了动画

    >>2)我猜这句话“Storyboard.SetTarget(animation, pl);” 和 “storyboard.Children.Add(animation);” 表示的是一个意思吧?

    不是一个意思,这是用于指定动画作用于什么目标作用对象,详见MSDN:http://msdn.microsoft.com/en-us/library/vstudio/cc663141(v=vs.110).aspx

    >>3)我想让这个流动,成为永动的。于是,试着在加了...但虚线还是只流动1秒。这怎么解决?

    问题是你的StoryBoard 没有启动,还有缺少设置目标对象和目标属性,对于我上面的代码,做如下改动:

    private void CreateStoryboard(double fromvalue, double tovalue, TimeSpan duration)
    {
                DoubleAnimation animation = new DoubleAnimation(fromvalue, tovalue, duration);
    
                Storyboard storyboard = new Storyboard();
                storyboard.Children.Add(animation);
                Storyboard.SetTarget(animation, pl);
                Storyboard.SetTargetProperty(animation, new PropertyPath("StrokeDashOffset"));
                storyboard.RepeatBehavior = RepeatBehavior.Forever;
                storyboard.Begin();
    }

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 OpenNovo 2014年10月1日 2:50
    2014年10月1日 1:18
    版主

全部回复

  • 你好,

    >>一直没明白怎么将 PolyLine.StrokeDashOffset的偏移量挂载动画上

    我们可以使用 DoubleAnimation 去改变StrokeDashOffset的值,实现你要的动画效果:

    <Canvas>
                <Polyline Points="100,50 10,50" Stroke="Black" StrokeThickness="4" StrokeDashArray="1, 1" StrokeDashOffset="0" >
                    <Polyline.Triggers>
                        <EventTrigger RoutedEvent="Polyline.MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation  Storyboard.TargetProperty="StrokeDashOffset"
                                                      From="0" To="10" Duration="0:0:1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Polyline.Triggers>
                </Polyline>
            </Canvas>

    当鼠标放在虚线上时,就会开始“流动”:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月30日 5:50
    版主
  • 问题是:这个,我怎么在代码里实现呢?我不想要XAML的实现。

    XAML 和 C#代码之间的转换,不太会。试了试,没能移植过去。多谢!

    2014年9月30日 6:26
  • 问题是:这个,我怎么在代码里实现呢?我不想要XAML的实现。

    XAML 和 C#代码之间的转换,不太会。试了试,没能移植过去。多谢!

    你好,

    >>这个,我怎么在代码里实现呢?我不想要XAML的实现

    以下是后台代码方式:

    private void CreateStoryboard(double fromvalue, double tovalue, TimeSpan duration)
    {
                DoubleAnimation animation = new DoubleAnimation(fromvalue, tovalue, duration);
                pl.BeginAnimation(Polyline.StrokeDashOffsetProperty, animation);
    
                Storyboard storyboard = new Storyboard();
                storyboard.Children.Add(animation);
                Storyboard.SetTarget(animation, pl);
    }
    
    private void pl_MouseEnter(object sender, MouseEventArgs e)
    {
                CreateStoryboard(0, 10, TimeSpan.FromSeconds(1));
    }

    XAML:

    <Canvas>
    <Polyline Name="pl" Points="100,50 10,50" Stroke="Black" StrokeThickness="4" StrokeDashArray="1, 1" StrokeDashOffset="0" MouseEnter="pl_MouseEnter" />
    </Canvas>


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月30日 7:59
    版主
  • 我试了试,能流动了。谢谢!

    1)但我不理解,为什么要加这句话:“Storyboard.SetTarget(animation, pl);”,我把这句话去掉,虚线也能流动。

    2)我猜这句话“Storyboard.SetTarget(animation, pl);” 和 “storyboard.Children.Add(animation);” 表示的是一个意思吧?

    3)我想让这个流动,成为永动的。于是,试着在加了:

    Polyline myLine = new Polyline();
    
    
    DoubleAnimation animation=new DoubleAnimation(4,0,TimeSpan.FromSeconds(1));            
                myLine .BeginAnimation (Polyline.StrokeDashOffsetProperty ,animation );            
                Storyboard board=new Storyboard();
                board.Children.Add (animation );
                //Storyboard.SetTarget (animation,myLine );
                board.RepeatBehavior = RepeatBehavior.Forever;
    
                MainCanvas.Children.Add(myLine);
    

    但虚线还是只流动1秒。这怎么解决?

    谢谢!

    2014年9月30日 8:51
  • 你好,

    >>1)但我不理解,为什么要加这句话:“Storyboard.SetTarget(animation, pl);”,我把这句话去掉,虚线也能流动。

    其实 pl.BeginAnimation(Polyline.StrokeDashOffsetProperty, animation); 就是开始了动画

    >>2)我猜这句话“Storyboard.SetTarget(animation, pl);” 和 “storyboard.Children.Add(animation);” 表示的是一个意思吧?

    不是一个意思,这是用于指定动画作用于什么目标作用对象,详见MSDN:http://msdn.microsoft.com/en-us/library/vstudio/cc663141(v=vs.110).aspx

    >>3)我想让这个流动,成为永动的。于是,试着在加了...但虚线还是只流动1秒。这怎么解决?

    问题是你的StoryBoard 没有启动,还有缺少设置目标对象和目标属性,对于我上面的代码,做如下改动:

    private void CreateStoryboard(double fromvalue, double tovalue, TimeSpan duration)
    {
                DoubleAnimation animation = new DoubleAnimation(fromvalue, tovalue, duration);
    
                Storyboard storyboard = new Storyboard();
                storyboard.Children.Add(animation);
                Storyboard.SetTarget(animation, pl);
                Storyboard.SetTargetProperty(animation, new PropertyPath("StrokeDashOffset"));
                storyboard.RepeatBehavior = RepeatBehavior.Forever;
                storyboard.Begin();
    }

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 OpenNovo 2014年10月1日 2:50
    2014年10月1日 1:18
    版主
  • 我试了一下,成功了;谢谢!

    我也试着理解这些句子:     
    Storyboard.SetTargetProperty(animation, new PropertyPath("StrokeDashOffset"));   // 我想:这句话运用了一个反射的概念,
    "StrokeDashOffset" 是 pl的属性,这就等于告诉动画“作用在哪个属性上”,这里可以用在“StrokeDashOffset”上,以后可以用在“X”上或对一个其它非WPF的double属性上,对吧?

    2014年10月1日 2:55
  • 我试了一下,成功了;谢谢!

    我也试着理解这些句子:     
    Storyboard.SetTargetProperty(animation, new PropertyPath("StrokeDashOffset"));   // 我想:这句话运用了一个反射的概念,
    "StrokeDashOffset" 是 pl的属性,这就等于告诉动画“作用在哪个属性上”,这里可以用在“StrokeDashOffset”上,以后可以用在“X”上或对一个其它非WPF的double属性上,对吧?


    是的:)

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月1日 2:57
    版主