【问题标题】:Strange error in code converted to VB.NET from C# [duplicate]从 C# 转换为 VB.NET 的代码中出现奇怪的错误 [重复]
【发布时间】:2012-10-11 06:07:52
【问题描述】:

可能重复: Method group in VB.NET?

在阅读an answer时,我得到了这个代码:

public static class Helper
{
    public static bool GetAutoScroll(DependencyObject obj)
    {
        return (bool)obj.GetValue(AutoScrollProperty);
    }

    public static void SetAutoScroll(DependencyObject obj, bool value)
    {
        obj.SetValue(AutoScrollProperty, value);
    }

    public static readonly DependencyProperty AutoScrollProperty =
        DependencyProperty.RegisterAttached("AutoScroll", typeof(bool),
        typeof(Helper),
        new PropertyMetadata(false, AutoScrollPropertyChanged));

    private static void AutoScrollPropertyChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        var scrollViewer = d as ScrollViewer;

        if (scrollViewer != null && (bool)e.NewValue)
        {
            scrollViewer.ScrollToBottom();
        }
    }
}

由于我在VB.NET中工作,所以我对其进行了转换并得到:

Public NotInheritable Class Helper

    Private Sub New()
    End Sub

    Public Shared Function GetAutoScroll(ByVal obj As DependencyObject)
    As Boolean
        Return CBool(obj.GetValue(AutoScrollProperty))
    End Function

    Public Shared Sub SetAutoScroll(ByVal obj As DependencyObject,
    ByVal value As Boolean)
        obj.SetValue(AutoScrollProperty, value)
    End Sub

    Public Shared ReadOnly AutoScrollProperty As DependencyProperty =
        DependencyProperty.RegisterAttached("AutoScroll", GetType(Boolean),
        GetType(Helper),
        New PropertyMetadata(False, AutoScrollPropertyChanged)) // Error Here

    Private Shared Sub AutoScrollPropertyChanged(ByVal d As
    System.Windows.DependencyObject, ByVal e As
    System.Windows.DependencyPropertyChangedEventArgs)
        Dim scrollViewer = TryCast(d, ScrollViewer)

        If scrollViewer IsNot Nothing AndAlso CBool(e.NewValue) Then
            scrollViewer.ScrollToBottom()
        End If
    End Sub

End Class

但是 C# 代码编译并且工作正常,但是在 VB.NET 中代码给出了一个错误(在代码中标记)说:

未为“Private Shared Sub AutoScrollPropertyChanged(d As System.Windows.DependencyObject, e As System.Windows.DependencyPropertyChangedEventArgs)”的参数“e”指定参数

我错过了什么? PropertyChangedCallback 委托正是它在对象浏览器中定义的方式:

Public Delegate Sub PropertyChangedCallback(
    ByVal d As System.Windows.DependencyObject, ByVal e As
    System.Windows.DependencyPropertyChangedEventArgs)

【问题讨论】:

标签: c# vb.net dependency-properties


【解决方案1】:

C# 有一个语言特性,可以将方法组转换为委托类型。所以,而不是:

private void Foo() {}
private void Bar(Action arg) {}

Bar(new Action(Foo));

你可以写:

Bar(Foo);

我不是 VB 专家,但我怀疑 VB .NET 没有这样的功能。 看来您需要AddressOf 运算符:

New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged)

【讨论】:

    【解决方案2】:

    我没有编译它,但我认为您应该使用 AddressOf 运算符参考 AutoScrollPropertyChanged:

    Public Shared ReadOnly AutoScrollProperty As DependencyProperty =
            DependencyProperty.RegisterAttached("AutoScroll", GetType(Boolean),
            GetType(Helper),
            New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged))
    

    【讨论】:

      猜你喜欢
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 2021-05-07
      • 2012-03-22
      • 2022-01-10
      • 2022-01-16
      相关资源
      最近更新 更多