2011-06-06 5 views
3

Может ли кто-нибудь дать мне пример того, как реализовать AttachedCommands?WPF AttachedCommandsBehavior в VB.NET

В C# есть несколько примеров, и я думаю, что он очень похож на C#, но у меня проблемы с переводом кода на VB.NET.

На данный момент мой попытаться перевести C# AttachedCommand-класса в VB.net выглядит следующим образом:

Imports System 
Imports System.Collections.Generic 
Imports System.Linq 
Imports System.Text 
Imports System.Windows 
Imports System.Windows.Input 
Imports System.Reflection 

Public Class AttachedCommand 
    Inherits DependencyObject 

#Region "CommandParameter-Class" 

    Private NotInheritable Class CommandParameter 

    Public Shared ReadOnly Property DefaultCP() As CommandParameter 
     Get 
      Return New CommandParameter() 
     End Get 
    End Property 



    Private _command As ICommand 
    Public Property Command() As ICommand 
     Get 
      Return _command 
     End Get 
     Set(ByVal value As ICommand) 
      _command = value 
     End Set 
    End Property 


    Private _eventName As String 
    Public Property EventName() As String 
     Get 
      Return _eventName 
     End Get 
     Set(ByVal value As String) 
      _eventName = value 
     End Set 
    End Property 


    Private _callBack As [Delegate] 
    Public Property Callback() As [Delegate] 
     Get 
      Return _callBack 
     End Get 
     Set(ByVal value As [Delegate]) 
      _callBack = value 
     End Set 
    End Property 

End Class 

#End Region 

#Region "Properties" 

Private Shared _parameter As IDictionary(Of DependencyObject, CommandParameter) 
Private Shared Property Parameter() As IDictionary(Of DependencyObject, CommandParameter) 
    Get 
     Return _parameter 
    End Get 
    Set(ByVal value As IDictionary(Of DependencyObject, CommandParameter)) 
     _parameter = value 
    End Set 
End Property 




Public Property Command() As ICommand 
    Get 
     Return GetValue(CommandProperty) 
    End Get 

    Set(ByVal value As ICommand) 
     SetValue(CommandProperty, value) 
    End Set 
End Property 

Public Shared ReadOnly CommandProperty As DependencyProperty = _ 
         DependencyProperty.Register("Command", _ 
         GetType(ICommand), GetType(AttachedCommand), _ 
         New UIPropertyMetadata(AddressOf CommandChanged)) 

Public Property EventName() As String 
    Get 
     Return GetValue(EventNameProperty) 
    End Get 

    Set(ByVal value As String) 
     SetValue(EventNameProperty, value) 
    End Set 
End Property 

Public Shared ReadOnly EventNameProperty As DependencyProperty = _ 
         DependencyProperty.Register("EventName", _ 
         GetType(String), GetType(AttachedCommand), _ 
         New UIPropertyMetadata(AddressOf EventNameChanged)) 


#End Region 

#Region "Event-Handler" 

Public Shared Sub CommandChanged(ByVal dependencyObject As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs) 
    If Not Parameter.ContainsKey(dependencyObject) Then 
     Parameter.Add(dependencyObject, CommandParameter.DefaultCP) 
    End If 

    If (Not Parameter(dependencyObject).Callback = Nothing) AndAlso (Not args.OldValue = Nothing) Then 
     _RemoveEventHandler(dependencyObject) 
    End If 

    Parameter(dependencyObject).Command = CType(args.NewValue, ICommand) 

    _AttachEventHandler(dependencyObject) 

End Sub 

Public Shared Sub EventNameChanged(ByVal dependencyObject As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs) 
    If Not Parameter.ContainsKey(dependencyObject) Then 
     Parameter.Add(dependencyObject, CommandParameter.DefaultCP) 
    End If 
End Sub 

#End Region 


#Region "Helper" 

Private Shared Sub _RemoveEventHandler(ByVal dependencyObject As DependencyObject) 
    If dependencyObject Is Nothing Then 
     Throw New ArgumentNullException("DependencyObject is null.") 
    End If 

    If Not Parameter.ContainsKey(dependencyObject) Then 
     Exit Sub 
    End If 

    Dim param As CommandParameter = Parameter(dependencyObject) 

    If param.Callback Is Nothing Then 
     Throw New InvalidProgramException("Cannot remove Callback. Callback is null.") 
    End If 

    Dim eventInfo As EventInfo = dependencyObject.GetType().GetEvent(param.EventName) 
    If eventInfo Is Nothing Then 
     Throw New InvalidProgramException(String.Format("Cannot find an event with the name <{0}>", param.EventName)) 
    End If 

    eventInfo.RemoveEventHandler(dependencyObject, param.Callback) 

End Sub 

Private Shared Sub _AttachEventHandler(ByVal dependencyObject) 
    If dependencyObject Is Nothing Then 
     Throw New ArgumentNullException("DependencyObject is null") 
    End If 

    If Not Parameter.ContainsKey(dependencyObject) Then 
     Exit Sub 
    End If 

    Dim param As CommandParameter = Parameter(dependencyObject) 

    If param.Command Is Nothing Or String.IsNullOrEmpty(param.EventName) Then 
     Exit Sub 
    End If 

    Dim eventInfo As EventInfo = dependencyObject.GetType.GetEvent(param.EventName) 
    If eventInfo Is Nothing Then 
     Throw New InvalidProgramException(String.Format("Cannot find an event with the name <{0}>", param.EventName)) 
    End If 



    eventInfo.AddEventHandler(dependencyObject, param.Callback) 

End Sub 

Private Shared Sub _CommandExecutAction(ByVal parameter As CommandParameter) 
    parameter.Command.Execute(Nothing) 
End Sub 

Private Shared Function _CreateHandler(ByVal eventInfo As EventInfo, ByVal method As Action) As [Delegate] 
    If eventInfo Is Nothing Then 
     Throw New ArgumentNullException("EventInfo is null") 
    End If 

    If method Is Nothing Then 
     Throw New ArgumentNullException("Action-method is null") 
    End If 

    Dim handlerType = eventInfo.EventHandlerType 
    Dim eventParams = handlerType.GetMethod("Invoke").GetParameters() 

    Dim parameters = eventParams.[Select](Function(p) System.Linq.Expressions.Expression.Parameter(p.ParameterType, "x")) 
    Dim body = System.Linq.Expressions.Expression.[Call](System.Linq.Expressions.Expression.Constant(method), method.[GetType]().GetMethod("Invoke")) 
    Dim lambda = System.Linq.Expressions.Expression.Lambda(body, parameters.ToArray()) 

    Return [Delegate].CreateDelegate(handlerType, lambda.Compile(), "Invoke", False) 

End Function 
#End Region 

#Region "INIT" 

Public Sub New() 
    Parameter = New Dictionary(Of DependencyObject, CommandParameter)() 
End Sub 

#End Region 

End Class 

Но теперь у меня есть проблема, что ПолучитьЗначение() и SetValue() - методы недоступен. Есть идеи?

Спасибо за вашу помощь ..

ответ

1

An AttachedCommandBehaviour не намного больше, чем свойства зависимостей типа ICommand, который hookes некоторых событий.

Отъезд Dr. WPFs VB-snippets, который включает в себя фрагменты для свойств зависимостей.

Если вы используете тот, который включает в себя обработчик с измененным свойством, вы можете использовать этот обработчик для подключения событий, которые вы хотите. В обработчике событий вы вызываете прилагаемую команду.

Если вы все еще не можете заставить его работать, отправьте код.

+0

Хорошо, есть код. Я попытался перевести его из класса C#. – rakete

+0

Вы пробовали http://www.developerfusion.com/tools/convert/csharp-to-vb/? – Vegar