我正在使用 KinectV2 和 Project Praga 来解释手势并执行诸如控制媒体、家庭自动化等操作。
当我的异步方法由手势触发时,需要打开另一个表单,但是当发生这种情况时,第二个表单崩溃了:
using Microsoft.Gestures;
using Microsoft.Gestures.Endpoint;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UIControl
{
public partial class KinectProject : Form
{
private static GesturesServiceEndpoint _gesturesService;
private static Gesture _One;
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Kinect Project Test";
}
public KinectProject()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e); InitializeGestures();
}
private async void InitializeGestures()
{
// One can optionally pass the hostname/IP address where the gestures service is hosted
var gesturesServiceHostName = "localhost";
await RegisterGestures(gesturesServiceHostName);
}
private static async Task RegisterGestures(string gesturesServiceHostName)
{
// Step 1: Connect to Microsoft Gestures service
_gesturesService = GesturesServiceEndpointFactory.Create(gesturesServiceHostName);
await _gesturesService.ConnectAsync();
// Step 2: Define bunch of custom Gestures, each detection of the gesture will emit some message into the console
await RegisterOneGesture();
}
private static async Task RegisterOneGesture()
{
// Start with defining the first pose, ...
var Iddle = new HandPose("iddle", new FingerPose(new AllFingersContext(), FingerFlexion.OpenStretched),
new PalmPose(new AnyHandContext(), PoseDirection.Backward));
var hold = new HandPose("Hold", new FingerPose(Finger.Index, FingerFlexion.Open, PoseDirection.Up),
new FingerPose(new[] { Finger.Thumb, Finger.Middle, Finger.Ring, Finger.Pinky }, FingerFlexion.Folded),
new PalmPose(new AnyHandContext(), PoseDirection.Backward));
// ... finally define the gesture using the hand pose objects defined above forming a simple state machine: hold -> rotate
_One = new Gesture("One", Iddle, hold);
_One.Triggered += (s, e) => Executor(s, e, ConsoleColor.DarkRed);
//_One.Triggered += (s, e) => OnGestureDetected(s, e, ConsoleColor.DarkRed);
// Step 3: Register the gesture
// Registering the like gesture _globally_ (i.e. isGlobal:true), by global registration we mean this gesture will be
// detected even it was initiated not by this application or if the this application isn't in focus
await _gesturesService.RegisterGesture(_One, isGlobal: true);
}
private static void OnGestureDetected(object sender, GestureSegmentTriggeredEventArgs args, ConsoleColor foregroundColor)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Gesture detected! : ");
Console.ForegroundColor = foregroundColor;
Console.WriteLine(args.GestureSegment.Name);
Console.ResetColor();
}
static void Executor(object sender, GestureSegmentTriggeredEventArgs args, ConsoleColor foregroundColor)
{
MusicForm V1 = new MusicForm();
V1.Show();
}
}
}
我最近添加了覆盖方法,但是相同的,当 MusicForm 打开时它崩溃,但如果我使用按钮,MusicForm 会正确打开
您需要使用 UI 线程来执行此类操作。 您可以使用 invoke 或 begininvoke。 尝试改变你的执行者
this.Invoke(() =>
{
MusicForm V1 = new MusicForm();
V1.Show();
});