问题
Possible Duplicate: Method group in VB.NET?
While reading an answer I got this code:
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();
}
}
}
Since I work in VB.NET, so I converted it and got:
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
But the C# code compiles and works fine, but in VB.NET the code gives an error (marked in code) saying:
Argument not specified for parameter 'e' of 'Private Shared Sub AutoScrollPropertyChanged(d As System.Windows.DependencyObject, e As System.Windows.DependencyPropertyChangedEventArgs)'
What am I missing? The PropertyChangedCallback
delegate is exactly the way it is defined in Object Browser:
Public Delegate Sub PropertyChangedCallback(
ByVal d As System.Windows.DependencyObject, ByVal e As
System.Windows.DependencyPropertyChangedEventArgs)
回答1:
C# has a language feature, that can convert method groups to delegate type. So, instead of:
private void Foo() {}
private void Bar(Action arg) {}
Bar(new Action(Foo));
you can write:
Bar(Foo);
I'm not a VB guy, but I suspect, that VB .NET hasn't such feature.
Looks like you need AddressOf operator:
New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged)
回答2:
I did not compile it, but I think you should refer AutoScrollPropertyChanged with the AddressOf operator:
Public Shared ReadOnly AutoScrollProperty As DependencyProperty =
DependencyProperty.RegisterAttached("AutoScroll", GetType(Boolean),
GetType(Helper),
New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged))
来源:https://stackoverflow.com/questions/12833213/strange-error-in-code-converted-to-vb-net-from-c-sharp