Dispatcher.BeginInvoke VB中的错误但不是C#

时间:2016-02-20 19:10:00

标签: c# vb.net ui-automation microsoft-ui-automation

嘿所有我正在使用一些用C#编写的UIAutomation代码。所以我把它转换成了VB.net,这样我就可以将它集成到我正在制作的程序中。

出现错误的代码是这一行:

Private Sub LogMessage(message As String)
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, New SetMessageCallback(AddressOf DisplayLogMessage), message)
End Sub

错误发生在 Dispatcher.BeginInvoke 上,说明错误1对非共享成员的引用需要对象引用。

C#中的代码如下所示:

private void LogMessage(string message)
{
    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetMessageCallback(DisplayLogMessage), message);
}

并且没有错误并且工作正常。

我在VB.net版本中缺少什么才能使其正常工作?

可以找到原始的C#代码HERE

我将C#转换为VB.net代码如下所示:

Imports System.Threading
Imports Automation = System.Windows.Automation
Imports System.Windows.Automation
Imports System.Windows.Threading

Public Class Form1
    Public Delegate Sub SetMessageCallback(ByVal [_Msg] As String)

    Private Sub Automate()
        LogMessage("Getting RootElement...")
        Dim rootElement As AutomationElement = AutomationElement.RootElement
        If rootElement IsNot Nothing Then
            LogMessage("OK." + Environment.NewLine)

            Dim condition As Automation.Condition = New PropertyCondition(AutomationElement.NameProperty, "UI Automation Test Window")

            LogMessage("Searching for Test Window...")
            Dim appElement As AutomationElement = rootElement.FindFirst(TreeScope.Children, condition)

            If appElement IsNot Nothing Then
                LogMessage("OK " + Environment.NewLine)
                LogMessage("Searching for TextBox A control...")
                Dim txtElementA As AutomationElement = GetTextElement(appElement, "txtA")
                If txtElementA IsNot Nothing Then
                    LogMessage("OK " + Environment.NewLine)
                    LogMessage("Setting TextBox A value...")
                    Try
                        Dim valuePatternA As ValuePattern = TryCast(txtElementA.GetCurrentPattern(ValuePattern.Pattern), ValuePattern)
                        valuePatternA.SetValue("10")
                        LogMessage("OK " + Environment.NewLine)
                    Catch
                        WriteLogError()
                    End Try
                Else
                    WriteLogError()
                End If

                LogMessage("Searching for TextBox B control...")
                Dim txtElementB As AutomationElement = GetTextElement(appElement, "txtB")
                If txtElementA IsNot Nothing Then
                    LogMessage("OK " + Environment.NewLine)
                    LogMessage("Setting TextBox B value...")
                    Try
                        Dim valuePatternB As ValuePattern = TryCast(txtElementB.GetCurrentPattern(ValuePattern.Pattern), ValuePattern)
                        valuePatternB.SetValue("5")
                        LogMessage("OK " + Environment.NewLine)
                    Catch
                        WriteLogError()
                    End Try
                Else
                    WriteLogError()
                End If
            Else
                WriteLogError()
            End If
        End If
    End Sub

    Private Function GetTextElement(parentElement As AutomationElement, value As String) As AutomationElement
        Dim condition As Automation.Condition = New PropertyCondition(AutomationElement.AutomationIdProperty, value)
        Dim txtElement As AutomationElement = parentElement.FindFirst(TreeScope.Descendants, condition)
        Return txtElement
    End Function

    Private Sub DisplayLogMessage(message As String)
        TextBox1.Text += message
    End Sub

    Private Sub LogMessage(message As String)
        Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New SetMessageCallback(AddressOf DisplayLogMessage), message)
    End Sub

    Private Sub WriteLogError()
        LogMessage("ERROR." + Environment.NewLine)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim automateThread As New Thread(New ThreadStart(AddressOf Automate))
        automateThread.Start()
    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

Dispatcher可以是静态的。我认为你需要Me.Dispatcher

答案 1 :(得分:1)

找到正确的方法:

Private Sub LogMessage(message As String)
    Me.BeginInvoke(New SetMessageCallback(AddressOf DisplayLogMessage), message)
End Sub

感谢@HansPassant的帮助。