Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 对方法调用Sub()_Vb.net - Fatal编程技术网

Vb.net 对方法调用Sub()

Vb.net 对方法调用Sub(),vb.net,Vb.net,最近我正在开发一个用vb.net编写的学校项目应用程序 我在使用XInput方法添加对操纵杆的支持时遇到问题。 我在上面找到密码了 但我认为它有一个小错误:当mogage.StateChanged事件引发时,我在MainWindow类上调用一个子类,但它说:调用线程无法访问该对象,因为它是另一个线程拥有的 Public WithEvents XJoy As XboxController = Nothing Public ReadOnly Property XInputJoy As XboxCon

最近我正在开发一个用vb.net编写的学校项目应用程序

我在使用XInput方法添加对操纵杆的支持时遇到问题。 我在上面找到密码了

但我认为它有一个小错误:当mogage.StateChanged事件引发时,我在MainWindow类上调用一个子类,但它说:调用线程无法访问该对象,因为它是另一个线程拥有的

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是基于轮询的,我们必须使用在另一个线程上运行的轮询循环。但是,我如何在该线程和我的主窗口(调用Sub)之间创建连接呢

非常感谢,很抱歉我的英语不好


我使用的是Visual Studio 2012,我的操纵杆类似于XBox(支持XInput)。

我假设您使用的是WPF,在这种情况下,您需要在UI线程上设置.Text属性。您可以使用,即

或通过调度器调用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

非常感谢您的回答,第一种方法“BeginInvoke”(Sub()…)有效,但第二种方法“BeginInvoke”(VV1)无效。此外,我还通过使用Dispatcher来执行循环代码而不是使用另一个线程来修复了这个问题。(可以吗??)很高兴听到您已经让它工作了:)Dispatcher也是一个很好的方法
Public Sub JoystickStateChanged(sender As Object, e As XboxControllerStateChangedEventArgs) Handles XJoy.StateChanged
    If XJoy.IsXPressed Then
       Me.Dispatcher.BeginInvoke(VV1)
    End If
End Sub