簡體   English   中英

如何:將匿名方法轉換為VB.NET

[英]How To: Convert anonymous method 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因為沒有返回類型不是函數,但是制作“子”我不知道如何...

一些忠告?

匿名方法的語法如下:

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才能使用它,以前的版本還不支持這個。

把它變成SUB。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM