2010-08-13 7 views
1

Quelqu'un peut-il m'aider à convertir cela en vb.net le convertisseur que j'ai utilisé ne fonctionne pas correctement et ne sais vraiment pas comment le convertir.C# à VB Conversion Question

public Person CurrentPersonCancellable 
    { 
     get 
     { 
      Debug.WriteLine("Getting CurrentPersonCancellable."); 
      return _CurrentPersonCancellable; 
     } 
     set 
     { 
      // Store the current value so that we can 
      // change it back if needed. 
      var origValue = _CurrentPersonCancellable; 

      // If the value hasn't changed, don't do anything. 
      if (value == _CurrentPersonCancellable) 
       return; 

      // Note that we actually change the value for now. 
      // This is necessary because WPF seems to query the 
      // value after the change. The combo box 
      // likes to know that the value did change. 
      _CurrentPersonCancellable = value; 

      if (
       MessageBox.Show(
        "Allow change of selected item?", 
        "Continue", 
        MessageBoxButton.YesNo 
       ) != MessageBoxResult.Yes 
      ) 
      { 
       Debug.WriteLine("Selection Cancelled."); 

       // change the value back, but do so after the 
       // UI has finished it's current context operation. 
       Application.Current.Dispatcher.BeginInvoke(
         new Action(() => 
         { 
          Debug.WriteLine(
           "Dispatcher BeginInvoke " + 
           "Setting CurrentPersonCancellable." 
          ); 

          // Do this against the underlying value so 
          // that we don't invoke the cancellation question again. 
          _CurrentPersonCancellable = origValue; 
          OnPropertyChanged("CurrentPersonCancellable"); 
         }), 
         DispatcherPriority.ContextIdle, 
         null 
        ); 

       // Exit early. 
       return; 
      } 

      // Normal path. Selection applied. 
      // Raise PropertyChanged on the field. 
      Debug.WriteLine("Selection applied."); 
      OnPropertyChanged("CurrentPersonCancellable"); 
     } 
    } 

Le convertisseur me donne ceci et où j'ai un problème est où il appelle la application.Current.Dispather.BeginInvoke.

Public Property CurrentPersonCancellable() As Person 
Get 
    Debug.WriteLine("Getting CurrentPersonCancellable.") 
    Return _CurrentPersonCancellable 
End Get 
Set 
    ' Store the current value so that we can 
    ' change it back if needed. 
    Dim origValue = _CurrentPersonCancellable 

    ' If the value hasn't changed, don't do anything. 
    If value = _CurrentPersonCancellable Then 
     Return 
    End If 

    ' Note that we actually change the value for now. 
    ' This is necessary because WPF seems to query the 
    ' value after the change. The combo box 
    ' likes to know that the value did change. 
    _CurrentPersonCancellable = value 

    If MessageBox.Show("Allow change of selected item?", "Continue", MessageBoxButton.YesNo) <> MessageBoxResult.Yes Then 
     Debug.WriteLine("Selection Cancelled.") 

     ' change the value back, but do so after the 
     ' UI has finished it's current context operation. 
     Application.Current.Dispatcher.BeginInvoke(New Action(Function() Do 
      Debug.WriteLine("Dispatcher BeginInvoke " + "Setting CurrentPersonCancellable.") 

      ' Do this against the underlying value so 
      ' that we don't invoke the cancellation question again. 
      _CurrentPersonCancellable = origValue 
      OnPropertyChanged("CurrentPersonCancellable") 
     End Function), DispatcherPriority.ContextIdle, Nothing) 

     ' Exit early. 
     Return 
    End If 

    ' Normal path. Selection applied. 
    ' Raise PropertyChanged on the field. 
    Debug.WriteLine("Selection applied.") 
    OnPropertyChanged("CurrentPersonCancellable") 
End Set 
End Property 
+4

Les gens pourraient être plus sensibles à votre question si vous soulignez un problème particulier que vous rencontrez plutôt que de demander à ce que votre travail soit fait pour vous. –

+2

S'il vous plaît trier votre mise en forme et essayer de réduire la quantité de code - tout ce code est vraiment nécessaire lorsqu'il apparaît que votre convertisseur a seulement un problème avec une ligne particulière? – GenericTypeTea

+2

"Le convertisseur me donne ceci et où j'ai un problème est où il appelle l'application.Current.Dispather.BeginInvoke." Et quel * est * le problème que vous rencontrez? Ce serait beaucoup plus facile de répondre si vous (a) seulement montré la partie pertinente du code, et (b) effectivement dit quel est votre problème. –

Répondre

1

Function utilisé dans le BeginInvoke ne peut pas contenir plusieurs déclarations.
Vous devez déplacer cette fonction dans une fonction séparée et appeler/prendre l'adresse appropriée.

Il y a beaucoup de choses (surtout à faire avec les lamdas et les méthodes anonymes) qu'on peut faire en C# que l'on ne peut tout simplement pas faire dans VB.Net.

De nombreux éléments de langage ne sont pas interchangeables.

+0

Merci pour votre aide. Peut-être que je dois le faire différemment. J'ai une liste à ma vue et j'utilise MVVM et je ne voulais rien dans le code, car cela va à l'encontre du but, mais je dois pouvoir annuler le selectionChanged dans la zone de liste s'ils n'y ont pas apporté de changements. Je dois le faire sur VB.net car mon entreprise ne permet pas d'autres langues. – spafa9

+0

@ spafa9: On dirait que vous avez besoin d'une autre entreprise

0

Try this ...

Public Property CurrentPersonCancellable() As Person 
    Get 
     Debug.WriteLine("Getting CurrentPersonCancellable.") 
     Return _CurrentPersonCancellable 
    End Get 
    Set 
     ' Store the current value so that we can ' 
     ' change it back if needed.' 
     Dim origValue = _CurrentPersonCancellable 

     ' If the value hasnt changed, dont do anything.' 
     If value = _CurrentPersonCancellable Then 
      Return 
     End If 

     ' Note that we actually change the value for now.' 
     ' This is necessary because WPF seems to query the ' 
     ' value after the change. The combo box' 
     ' likes to know that the value did change.' 
     _CurrentPersonCancellable = value 

     If MessageBox.Show("Allow change of selected item?", "Continue", MessageBoxButton.YesNo) <> MessageBoxResult.Yes Then 
      Debug.WriteLine("Selection Cancelled.") 

      ' change the value back, but do so after the ' 
      ' UI has finished its current context operation.' 
      Application.Current.Dispatcher.BeginInvoke(New Action(of Person)(addressof dispatcherCallerHelper), _ 
                 DispatcherPriority.ContextIdle, _ 
                 new Object() {origValue}) 

      ' Exit early. ' 
      Return 
     End If 

     ' Normal path. Selection applied. ' 
     ' Raise PropertyChanged on the field.' 
     Debug.WriteLine("Selection applied.") 
     OnPropertyChanged("CurrentPersonCancellable") 
    End Set 
End Property 

private sub dispatcherCallerHelper(origValue as Person) 
    Debug.WriteLine("Dispatcher BeginInvoke " & "Setting CurrentPersonCancellable.") 

    ' Do this against the underlying value so ' 
    ' that we dont invoke the cancellation question again.' 
    _CurrentPersonCancellable = origValue 
    OnPropertyChanged("CurrentPersonCancellable") 
end sub 

Sur une note de côté, pour votre fonction OnPropertyChanged, vous devriez envisager d'utiliser la réflexion statique:

http://www.codeproject.com/Articles/36262/Getting-Fun-with-NET-Static-Reflection.aspx

Cheers!

+0

Merci! J'y jetterais un œil. – spafa9