最近我正在研究用vb.net编写的学校项目应用程序。
我在使用XInput Method添加对My Joystick的支持时遇到问题。 我在http://www.codeproject.com/Articles/492473/Using-XInput-to-access-an-Xbox-Controller-in-M找到了一个代码?
但它有一个小错误我认为:当Joystick.StateChanged事件引发时,我在我的MainWindow类上调用Sub但它说:调用线程无法访问此对象,因为另一个线程拥有它。
Public WithEvents XJoy As XboxController = Nothing
Public ReadOnly Property XInputJoy As XboxController
Get
Return XJoy
End Get
End Property
Public Sub JoystickStateChanged(sender As Object, e As XboxControllerStateChangedEventArgs) Handles XJoy.StateChanged
If XJoy.IsXPressed Then
VV1()
End If
End Sub
Public Sub VV1()
TEST1_Text.Text = "X Pressed"
End Sub
Public Sub WindowLoaded()
XJoy = XboxController.RetrieveController(0)
XboxController.StartPolling()
If XJoy.IsConnected Then
MessageBox.Show("Found Joystick In XInput Mode !")
End If
End Sub
请注意,Xinput基于轮询,我们必须使用在另一个线程上运行的轮询循环。但是如何在该线程和我的主窗口之间创建连接(Call a Sub)???
非常感谢,抱歉我的英语不好
我正在使用Visual Studio 2012,而我的操纵杆就像XBox一样(支持XInput。)
答案 0 :(得分:1)
我假设你正在使用WPF,在这种情况下你需要在UI线程上设置.Text属性。您可以使用Dispatcher.BeginInvoke,即
执行此操作Public Sub VV1()
Me.Dispatcher.BeginInvoke(Sub() TEST1_Text.Text = "X Pressed")
End Sub
或通过Dispatcher调用VV1函数:
Public Sub JoystickStateChanged(sender As Object, e As XboxControllerStateChangedEventArgs) Handles XJoy.StateChanged
If XJoy.IsXPressed Then
Me.Dispatcher.BeginInvoke(VV1)
End If
End Sub