首页 > WPF VB 在别的线程里改UI

WPF VB 在别的线程里改UI

我利用了一个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

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

【热门文章】
【热门文章】