C#到VB.net转换修复

时间:2014-08-09 03:10:07

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

嘿,我已经转换了以下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

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

我需要做的就是将功能重命名为 sub

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

由@SLaks here

回答

现在它运作得很好!