2012-03-20 70 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