我在向项目添加点击事件时遇到了一些问题。主要逻辑是正确的,但我需要一些点击事件的帮助。 该项目在Canvas上放置了一些按钮,我想在它们上添加一个click事件。
我正在使用Kinect v2,每次都找到一个Body,每次都找到一个SpineMid关节,项目会使用正多边形的形状在身体周围绘制一些按钮。
在我的情况下,问题是我无法点击按钮,因为它们在每一帧都被清除。
每次我找到关节时都会调用函数InitializeButtons(所以,我每帧创建新按钮然后清除它们)所以基本上不可能点击它们,没有时间查看按钮的内容变化:他们立即被替换了!
这是我调用函数的代码:
foreach (Body body in bodies){
foreach (var item in body.Joints){
JointType jointType = item.Key;
Joint joint = item.Value;
if (joint.JointType == JointType.SpineMid) {
InitializeButtons(100, 200, center, verticies, 300)
}
}
}
这是InizializeButtons函数的代码:
private void InitializeButtons(int height, int width, Point center, Point[] verticies, int radius)
{
Button test_button1 = new Button() { Background = Brushes.Aquamarine, Height = height, Width = width, Content = "I'm test button1!" };
Button test_button2 = new Button() { Background = Brushes.Aqua, Height = height, Width = width, Content = "I'm test button2!" };
Button test_button3 = new Button() { Background = Brushes.Beige, Height = height, Width = width, Content = "I'm test button3!" };
Button test_button4 = new Button() { Background = Brushes.BurlyWood, Height = height, Width = width, Content = "I'm test button4!" };
Button test_button5 = new Button() { Background = Brushes.Bisque, Height = height, Width = width, Content = "I'm test button5!" };
test_button1.Click += test_button1_Click;
canvas.Children.Add(test_button1);
canvas.Children.Add(test_button2);
canvas.Children.Add(test_button3);
canvas.Children.Add(test_button4);
canvas.Children.Add(test_button5);
Canvas.SetLeft(test_button1, 0);
Canvas.SetTop(test_button1, 0);
Canvas.SetLeft(test_button2, 0);
Canvas.SetTop(test_button2, 0);
Canvas.SetLeft(test_button3, 0);
Canvas.SetTop(test_button3, 0);
Canvas.SetLeft(test_button4, 0);
Canvas.SetTop(test_button4, 0);
Canvas.SetLeft(test_button5, 0);
Canvas.SetTop(test_button5, 0);
buttons = new List<Button> { test_button1, test_button2, test_button3, test_button4, test_button5 };
verticies = Polygon.CalculateVertices(buttons.Count, radius, 18, center);
int i = 0;
foreach (Button button in buttons)
{
Canvas.SetLeft(button, verticies[i].X);
Canvas.SetTop(button, verticies[i].Y);
i++;
}
verticies = null;
}
void test_button1_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = (Button)sender;
clickedButton.Content = "let's try it";
}
有什么方法可以超越它吗? 任何帮助都非常感谢。
由于