Nik*_*wal 2 c# vb.net dependency-properties
可能重复: VB.NET中的方法组?
在阅读答案时我得到了这段代码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
因为我在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
Run Code Online (Sandbox Code Playgroud)
但是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)
Run Code Online (Sandbox Code Playgroud)
C#具有语言功能,可以将方法组转换为委托类型.所以,而不是:
private void Foo() {}
private void Bar(Action arg) {}
Bar(new Action(Foo));
Run Code Online (Sandbox Code Playgroud)
你可以写:
Bar(Foo);
Run Code Online (Sandbox Code Playgroud)
我不是VB人,但我怀疑,VB .NET没有这样的功能.看起来你需要AddressOf运算符:
New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged)
Run Code Online (Sandbox Code Playgroud)