简体   繁体   English

C#移动折线

[英]C# move a Polyline

I'm new in the graphical programming with C#. 我是C#图形编程的新手。 Yesterday I started one new project (WPF). 昨天我开始了一个新项目(WPF)。 There is a Polyline object which I have to move along the screen with coordinates which I'm calculating. 有一个Polyline对象,我必须沿着屏幕移动我正在计算的坐标。 I don't know how to move the object and make something like an animation. 我不知道如何移动对象并制作类似动画的东西。 On mouse down I want to start this method Move() after that go into the while cycle and when the condition is completed (end == true) I want to end the cycle and complete the animation. 在鼠标按下时我想启动此方法Move()之后进入while循环并且条件完成时(end == true)我想结束循环并完成动画。 And while I'm in the cycle my idea is to move my Polyline with slow movements. 当我在循环中时,我的想法是以慢动作移动我的折线。 I tried to put Move() into a thread and use Thread.Sleep(...); 我试图将Move()放入一个线程并使用Thread.Sleep(...); but I could see just the end position not all way of the Polyline. 但我只能看到Polyline的最终位置。 I tried to put it into new Thread(new ThreadStart(Move)); 我试着将它放入new Thread(new ThreadStart(Move)); ...and this.Dispatcher.BeginInvoke , the effect was the same. ...和this.Dispatcher.BeginInvoke ,效果是一样的。 Could you tell me, please, how I can make this movement? 你能告诉我,我怎么能做这个动作?

     public void Move()
     {
        bool end = false;
        while (!end)
        {
            double x = lastPosX;
            double y = lastPosY;

            double a = y1 - y;
            double b = x - x1;
            double c = -x * y1 + x1 * y;

            double u, v;
            GetC(out u, out v);                    

            if (y1 < lastPosY)
            {
                GetCoordinates(ref u, ref v);
            }

            if (u > width || v > height)
            {
                 gameEnd = true;
            }

            lastPosX = u;
            lastPosY = v;

            p.Points.Remove(p.Points.First());
            p.Points.Add(new Point(u, v));

          }

       }

I wasn't quite able to figure out how your Move method works but here's an example of how you can move a Polyline from Left to Right upon MouseDown. 我无法弄清楚你的Move方法是如何工作的,但这里是一个如何在MouseDown上将Polyline从左向右移动的示例。 Hopefully you'll be able to adapt it to your needs 希望您能够根据自己的需求进行调整

Xaml XAML

<Canvas Name="myCanvas">
    <Polyline Name="myPolyline"
              MouseDown="Polyline_MouseDown"
              Canvas.Left="75"
              Canvas.Top="50"
              Points="25,25 0,50 25,75 50,50 25,25 25,0" 
              Stroke="Blue"
              StrokeThickness="10"/>
</Canvas>

Code behind 代码背后

private void Polyline_MouseDown(object sender, MouseButtonEventArgs e)
{
    double left = Canvas.GetLeft(myPolyline);
    var animationThread = new Thread(new ThreadStart(() =>
    {
        while (left < 300)
        {
            left += 10;
            // SetLeft is done in the UI thread
            Dispatcher.Invoke(new Action(() =>
            {
                Canvas.SetLeft(myPolyline, left);
            }));
            Thread.Sleep(50);
        }
    }));
    animationThread.Start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM