c# - 如何 : Convert anonymous method to VB. NET

标签 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”我不知道如何...

一些建议?

最佳答案

匿名方法的语法如下:

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才能使用这个,以前的版本还不支持这个。

关于c# - 如何 : Convert anonymous method to VB. NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784285/

相关文章:

c# - Nuget 迁移的 Svn 外部 - 关注点

c# - BackgroundAudioPlayer 没有开始播放

c# - 将 Lambda 表达式树转换预编译为常量?

xml - Json格式转xml格式

vb.net - 无法跨程序集边界使用,因为它具有属于嵌入式互操作类型的泛型类型参数

vb.net - 在.NET环境下编译VB6代码

c# - Process.Start() 什么都不做

c# - 如何避免替换 "_x0020_"的空间?

c# - 将不同版本的程序集加载到单独的 AppDomain 中

c# - 无法在 Roslyn 的新 nuget 中获取方法 SyntaxTree.ParseFile?