通过将省略号添加为 children,在 canvas 上不显示,但在 XAML 中添加时显示
Ellipses not shown on canvas by adding them as children but do when added in XAML
如标题所述,我的应用程序中有一个 wiimote,它提供了需要显示椭圆的像素坐标。然而,问题是当我用 Canvas.Children.Add()
添加它们时,它们不会出现在 canvas 上。
但是,如果我在 canvas 中添加省略号 XAML,然后在每次遥控器提供新的 x,y 坐标时修改它们的坐标,那么省略号会出现在 canvas 并且似乎正在改变位置。
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");
}
我发现您忘记在 DrawSensorMethod
中为椭圆传感器添加填充 属性
sensor.Fill = new SolidColorBrush(color);
但更重要的是,我鼓励您不要每次都创建新的椭圆,只需创建 4 个或您需要的数量,然后使用 TranslateTransform 移动它们,它使用 GPU 而不是 CPU,效果是顺利
如标题所述,我的应用程序中有一个 wiimote,它提供了需要显示椭圆的像素坐标。然而,问题是当我用 Canvas.Children.Add()
添加它们时,它们不会出现在 canvas 上。
但是,如果我在 canvas 中添加省略号 XAML,然后在每次遥控器提供新的 x,y 坐标时修改它们的坐标,那么省略号会出现在 canvas 并且似乎正在改变位置。
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");
}
我发现您忘记在 DrawSensorMethod
中为椭圆传感器添加填充 属性sensor.Fill = new SolidColorBrush(color);
但更重要的是,我鼓励您不要每次都创建新的椭圆,只需创建 4 个或您需要的数量,然后使用 TranslateTransform 移动它们,它使用 GPU 而不是 CPU,效果是顺利