如何:将匿名方法转换为VB.NET

时间:2012-03-20 09:35:53

标签: c# .net vb.net anonymous-function c#-to-vb.net

我在C#中有以下内容:

public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, double toValue, double animationDurationSeconds, EventHandler completedEvent)
{
    double fromValue = (double)animatableElement.GetValue(dependencyProperty);

    DoubleAnimation animation = new DoubleAnimation();
    animation.From = fromValue;
    animation.To = toValue;
    animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);

    //// HERE ----------------------------------------------------
    animation.Completed += delegate(object sender, EventArgs e)
    {
        //
        // When the animation has completed bake final value of the animation
        // into the property.
        //
        animatableElement.SetValue(dependencyProperty,
                                 animatableElement.GetValue(dependencyProperty));
        CancelAnimation(animatableElement, dependencyProperty);

        if (completedEvent != null)
        {
            completedEvent(sender, e);
        }
    };

将匿名方法转换为VB.NET时遇到了一些问题。

我的变体是

  AddHandler animation.Completed,
    Function(sender As Object, e As EventArgs) As Boolean
      ' '
      ' When the animation has completed bake final value of the animation '
      ' into the property. '
      '
      animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty))
      CancelAnimation(animatableElement, dependencyProperty)

      completedEvent(sender, e)
      Return True
    End Function

我添加了Function As Boolean因为没有返回类型不是函数,但是制作“Sub”我不知道如何...

一些建议?

2 个答案:

答案 0 :(得分:5)

匿名方法的语法如下:

AddHandler animation.Completed, _
    Sub(sender As Object, e As EventArgs)
      ' '
      ' When the animation has completed bake final value of the animation '
      ' into the property. '
      '
      animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty))
      CancelAnimation(animatableElement, dependencyProperty)

      completedEvent(sender, e)
    End Sub

但是,你需要VB10才能使用它,以前的版本还不支持这个。

答案 1 :(得分:-1)

将其设为SUB。

Public SUB <method name>(<parameters>)
'Body of the method