[英]C# move a Polyline
我是C#圖形編程的新手。 昨天我開始了一個新項目(WPF)。 有一個Polyline對象,我必須沿着屏幕移動我正在計算的坐標。 我不知道如何移動對象並制作類似動畫的東西。 在鼠標按下時我想啟動此方法Move()
之后進入while循環並且條件完成時(end == true)
我想結束循環並完成動畫。 當我在循環中時,我的想法是以慢動作移動我的折線。 我試圖將Move()
放入一個線程並使用Thread.Sleep(...);
但我只能看到Polyline的最終位置。 我試着將它放入new Thread(new ThreadStart(Move));
...和this.Dispatcher.BeginInvoke
,效果是一樣的。 你能告訴我,我怎么能做這個動作?
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));
}
}
我無法弄清楚你的Move方法是如何工作的,但這里是一個如何在MouseDown上將Polyline從左向右移動的示例。 希望您能夠根據自己的需求進行調整
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>
代碼背后
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.