WPF VB 在别的线程里改UI

发布于 2022-09-01 21:43:53 字数 1002 浏览 17 评论 0

我利用了一个Timer来扫描Xbox 360控制器的按键
但是不知道怎么改动UI
错误信息:

An exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll but was not handled in user code
Additional information: The calling thread cannot access this object because a different thread owns it.


Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Input
Imports System.Timers
Imports System.Windows.Threading

Public Class XboxControllerStatus
    Friend WithEvents Timer1 As Timer
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)                 Handles Timer1.Elapsed

        Dim currentState As GamePadState = GamePad.GetState(PlayerIndex.One)
        If currentState.IsConnected Then
            If currentState.Buttons.LeftShoulder.Pressed Then
                LB.Content = "Pressed"

            Else
                LB.Content = "LB"
            End If
        End If

    End Sub

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待"谢繁草 2022-09-08 21:43:53

跨线程修改UI线程需要使用UI线程的Dispatcher.BeginInvoke或Invoke方法。

故事灯 2022-09-08 21:43:53

看来碰vb的真的很少.我给你答案吧.
Imports System.Threading

Class MainWindow

Dim Thread0 As Thread
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Thread0 = New Thread(AddressOf Thread_0)
    Thread0.Start()

End Sub
Private Sub Thread_0()
    Dim i As Integer = 2

    Dispatcher.BeginInvoke(New UpdateLabelDelegate(AddressOf UpUI), i, "testtext")
End Sub


Private Delegate Sub UpdateLabelDelegate(ByVal i As Integer, ByVal s As String)

Private Sub UpUI(ByVal i As Integer, ByVal s As String)
    Select Case i
        Case 0
        Case 1
            textbox1.Text = s
        Case 2
            TextBlock1.Text = s
    End Select
End Sub

End Class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文