执行更新GUI的代码,但GUI永远不会更新

时间:2017-06-28 21:05:00

标签: .net vb.net winforms user-interface

我现有的VB代码在智能设备(WindowsCE)上运行。我编辑了代码以获得"模拟模式"它根据预处理器指令编译windows而不是CE。为了允许运行模拟设备的用户在不读取日志文件的情况下了解正在发生的事情,我正在尝试添加UI。目前,任何用于更新UI的代码都在执行,但更改永远不会出现。

这是我到目前为止所尝试的内容:

Public Module SomeDevice

#If __SIMULATE__ Then
    Private WithEvents _derp As New SimulatorGUI()
    Private _guiShown As Boolean = False
    Private _guiLoading As Boolean = False

    Private Sub handleShown(sender As Object, e As EventArgs) Handles _derp.Shown
        _guiShown = True
    End Sub

    Private Sub handleLoading(sender As Object, e As EventArgs) Handles _derp.Load
        _guiLoading = True
    End Sub

#End If

   Public Sub Main()
   #If __SIMULATE__ Then
        Dim GUIThread As Threading.Thread = New Threading.Thread(Sub() _derp.ShowDialog()) 
        'I have also tried System.Windows.Forms.Application.Run(_derp))

        GUIThread.Name = "Simulator GUI"
        GUIThread.Start()

        While Not _guiLoaded OrElse Not _guiShown
            Thread.Sleep(0)
        End While

        SimulatorGUI.PutOnGUI("Loaded and shown")

 #End If
 'normal device functionality
   End Sub
End Module

在SimulatorGUI类中:

Public Class SimulatorGUI
    Public Sub PutOnGUI(ByVal message As String)
        If Me.InvokeRequired Then
            Me.Invoke(Sub() PutOnGUI(message))
        Else
            ListBox1.Items.Add(message)
            Debug.WriteLine("DOING STUFF: " + message)
            'ListBox1.Invalidate()
            ListBox1.Update()
        End If
    End Sub
End Class

如果我运行此代码,Debug打印出现,列表框内部集合显示它包含预期的文本,但文本从未显示在GUI上。此外," InvokeRequired"检查永远不会导致对Me.Invoke的调用。如果重要,设备代码的其余部分将完全按预期运行。这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

您没有从您创建的实例中调用代码。

尝试将代码更改为:

'SimulatorGUI.PutOnGUI("Loaded and shown")
_derp.PutOnGUI("Loaded and shown")