2012-10-11 121 views
2

可能重複轉換到VB.NET: Method group in VB.NET?奇怪的錯誤從C#

在閱讀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代碼給出了一個錯誤(標記代碼)說:爲「私人共享子AutoScrollPropertyChanged參數「E」沒有指定

參數(d作爲System.Windows.DependencyObject,E作爲System.Windows.DependencyPropertyChangedEventArgs)」

Eenter image description here

我缺少什麼?該PropertyChangedCallback代表正是它在對象瀏覽器中定義的方式:

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

參閱關於在VB方法組的其他問題。淨[VB.Net中的方法組](http://stackoverflow.com/questions/12803897/method-group-in-vb-net)和[*模糊標題*實際上關於方法組](http:// stackoverflow。 com/questions/8047249/how-to-do-this-in-vb-net) – MarkJ

回答

8

C#有一個語言功能,它可以將方法組委託類型。因此,而不是:

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

Bar(new Action(Foo)); 

你可以寫:

Bar(Foo); 

我不是一個VB的傢伙,但我懷疑,即VB .NET並沒有這樣的功能。 看起來像你需要AddressOf操作:

New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged) 
+0

傻了。有效。謝謝 –

4

我沒編譯它,但我認爲你應該參考AutoScrollPropertyChanged與AddressOf運算符:

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

傻我。有效。謝謝 –