2012-03-20 72 views
4

在C#我有以下幾點:如何:轉換匿名方法VB.NET

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因爲沒有返回類型是不是一個函數,而是做一個「子」我不知道該怎麼...

一些建議嗎?

回答

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能夠利用這一點,以前的版本不支持這個呢。

+0

@moldovanu是的,應該工作。 – 2012-03-20 09:45:57

+0

我試過了,但我想我還有其他一些讓我迷失方向的錯誤。謝謝! ) – moldovanu 2012-03-20 09:56:12

-1

使它成爲一個SUB。

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