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

mol*_*anu 4 .net c# 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);
        }
    };
Run Code Online (Sandbox Code Playgroud)

我有一些问题将匿名方法转换为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
Run Code Online (Sandbox Code Playgroud)

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

一些忠告?

Kon*_*lph 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
Run Code Online (Sandbox Code Playgroud)

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