c#到VB.net的转换修复

本文关键字:转换 net VB | 更新日期: 2023-09-27 18:17:07

大家好,我已经转换了下面的c#代码:

if (Common.StartTask(() => Common.ClickButtonNtimes("SmallDecrement", "QuantityUpDown", 99))) return;

到VB.net

If Common.StartTask(Function() Common.ClickButtonNtimes("SmallDecrement", "QuantityUpDown", 99)) Then
    Return
End If

然而,我在

行上得到一个错误
Common.ClickButtonNtimes("SmallDecrement", "QuantityUpDown", 99))

的VB代码:

表达式不产生值。

c#中被调用的类看起来像这样:

internal static void ClickButtonNtimes(string automationId, string windowId, int count)
{
        try
        {
            var condition = new PropertyCondition(AutomationElement.AutomationIdProperty, windowId);
            var button = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, condition);
            condition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationId);
            button = button.FindFirst(TreeScope.Subtree, condition);
            InvokePattern clickButton = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
            for (int i = 1; i < count; i++)
                clickButton.Invoke();
        }
        catch (Exception)
        {
            MessageBox.Show(ERROR + automationId);
            Thread.Sleep(_timeout);
        }
    }

并将上述类转换为VB.net:

Friend Shared Sub ClickButtonNtimes(automationId As String, windowId As String, count As Integer)
        Try
            Dim condition = New PropertyCondition(AutomationElement.AutomationIdProperty, windowId)
            Dim button = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, condition)
            condition = New PropertyCondition(AutomationElement.AutomationIdProperty, automationId)
            button = button.FindFirst(TreeScope.Subtree, condition)
            Dim clickButton As InvokePattern = DirectCast(button.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)
            For i As Integer = 1 To count - 1
                clickButton.Invoke()
            Next
        Catch generatedExceptionName As Exception
            MessageBox.Show([ERROR] & automationId)
            Thread.Sleep(_timeout)
        End Try
    End Sub

任何帮助将是伟大的!

c#到VB.net的转换修复

我所需要做的就是将函数重命名为:

If Common.StartTask(Sub() Common.ClickButtonNtimes("SmallDecrement", "QuantityUpDown", 99)) Then
     Return
End If

@SLaks的回答

现在它工作得很好!