正如标题所述,我在我的应用程序中有一个wiimote,它提供了椭圆需要显示的像素坐标。但问题是,当我用Canvas.Children.Add()
添加它们时,它们不会出现在画布上。
但是,如果我使用XAML在画布中添加椭圆,然后每次遥控器提供新的x,y坐标时修改它们的坐标,那么椭圆就会出现在画布上并且似乎正在改变位置。
if (remote == service.wiimote1_info)
{
Action action = () =>
{
WM1Status_Lbl.Content = remote.Astatus.ToString();
WM1_Accel_X.Content = remote.Accelerometer[0].ToString();
WM1_Accel_Y.Content = remote.Accelerometer[1].ToString();
WM1_Accel_Z.Content = remote.Accelerometer[2].ToString();
WM1_IR1.Content = String.Concat(" X = ", remote.IRState[0, 0]/10, "; Y = ", remote.IRState[0, 1]/10, "; Size = ", remote.IRState[0, 2]);
WM1_IR2.Content = String.Concat(" X = ", remote.IRState[1, 0]/10, "; Y = ", remote.IRState[1, 1]/10, "; Size = ", remote.IRState[1, 2]);
WM1_IR3.Content = String.Concat(" X = ", remote.IRState[2, 0]/10, "; Y = ", remote.IRState[2, 1]/10, "; Size = ", remote.IRState[2, 2]);
WM1_IR4.Content = String.Concat(" X = ", remote.IRState[3, 0]/10, "; Y = ", remote.IRState[3, 1]/10, "; Size = ", remote.IRState[3, 2]);
wm1_progressbar.Value = remote.battery;
//IRImage.Children.Clear();
Canvas.SetLeft(IRSensor1, remote.IRState[0, 0] ) ; Canvas.SetBottom(IRSensor1, remote.IRState[0, 1]);
Canvas.SetLeft(IRSensor2, remote.IRState[1, 0]); Canvas.SetBottom(IRSensor2, remote.IRState[1, 1]);
Canvas.SetLeft(IRSensor3, remote.IRState[2, 0] ); Canvas.SetBottom(IRSensor3, remote.IRState[2, 1] );
Canvas.SetLeft(IRSensor4, remote.IRState[3, 0]); Canvas.SetBottom(IRSensor4, remote.IRState[3, 1]);
//At first I tryed to add the sensors with my DrawSensor() method , but it did not work . So then I have decided to modify already existing sensors instead of continuousely creating new ones .
DrawSensor(System.Drawing.Color.Blue, remote.IRState[0, 0]/10, remote.IRState[0,1]/10, (int)remote.IRState[0, 2]+1);
DrawSensor(System.Drawing.Color.Red, remote.IRState[1, 0]/10, remote.IRState[1, 1]/10, (int)remote.IRState[1, 2]+1);
DrawSensor(System.Drawing.Color.Yellow, remote.IRState[2, 0]/10, remote.IRState[2, 1]/10, (int)remote.IRState[2, 2]+1);
DrawSensor(System.Drawing.Color.Green, remote.IRState[3, 0]/10, remote.IRState[3, 1]/10, (int)remote.IRState[3, 2]+1);
};
Dispatcher.Invoke(action);
updated = true;
}
public void DrawSensor(System.Drawing.Color color , double x , double y , int Size )
{
Ellipse sensor = new Ellipse();
sensor.Width = Size + 1;
sensor.Height = Size + 1;
sensor.Opacity = 100;
Canvas.SetTop(sensor , x);
Canvas.SetBottom(sensor , y);
Canvas.SetLeft(sensor, y);
IRImage.Children.Add(sensor);
Console.WriteLine("Point drawn");
}
答案 0 :(得分:0)
我已经看到你忘记在DrawSensorMethod中添加Ellipse传感器的Fill属性
sensor.Fill = new SolidColorBrush(color);
但更重要的是,我鼓励你不要每次创建新的省略号,只需创建4或你需要的金额,并使用TranslateTransform移动它们,它使用GPU而不是CPU,效果很流畅