2016-01-27 19 views
5

Ho appena iniziato con WPF alcuni giorni fa e ho riscontrato un problema che non capisco.Valore non può essere nullo CommandBinding comando personalizzato

ho ottenuto il seguente errore:

Value cannot be null. Parametername: value

L'errore si verifica qui:

<Window.CommandBindings> 
     <CommandBinding Command="self:CustomCommands.Exit" Executed="ExitCommand_Executed" CanExecute="ExitCommand_CanExecute"/> 
</Window.CommandBindings> 

ho naturalmente insieme namespace xmlns:self="clr-namespace:PrintMonitor" in XAML.

Il code-behind:

namespace PrintMonitor 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
     { 
      if(e != null) 
       e.CanExecute = true; 
     } 

     private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e) 
     { 
      Application.Current.Shutdown(); 
     } 
    } 

    public static class CustomCommands 
    { 
     public static readonly RoutedUICommand Exit = new RoutedUICommand 
       (
         "Beenden", 
         "Exit", 
         typeof(CustomCommands), 
         new InputGestureCollection() 
         { 
          new KeyGesture(Key.F4, ModifierKeys.Alt) 
         } 
       ); 
    } 
} 

Allora perché questo errore si verifica se si utilizza un comando personalizzato, ma non se uso per esempio Command="ApplicationCommands.New" e come posso risolvere questo errore?

Il codice fa parte di this tutorial.

+0

Quale versione di VS stai usando? Non sono in grado di riprodurre il tuo errore in base al tutorial e agli snippet forniti. – Geoffrey

+1

Vs 2015 Enterprise V 14.0. Dovrei aggiungere che il progetto viene compilato ed eseguito ma l'errore persiste –

+1

Ora ho creato un nuovo progetto e copiato 1: 1 e l'errore è scomparso ... bug intellisense credo !? –

risposta

0

Forse è necessario impostare le CustomCommands di non statica,

e lasciare DataContext del MainWindow di essere CustomCommands

public class CustomCommands 
{ 
     public static readonly RoutedUICommand Exit = new RoutedUICommand 
       (
         "Beenden", 
         "Exit", 
         typeof(CustomCommands), 
         new InputGestureCollection() 
         { 
          new KeyGesture(Key.F4, ModifierKeys.Alt) 
         } 
       ); 
} 

public partial class MainWindow : Window 
{ 
    public CustomCommands CM; 
    public MainWindow() 
    { 
     CM = new CustomCommands(); 
     this.DataContext = CM; 
     InitializeComponent(); 

    } 

    private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
    { 
     if(e != null) 
      e.CanExecute = true; 
    } 

    private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e) 
    { 
     Application.Current.Shutdown(); 
    } 

} 
Problemi correlati