2009-07-09 4 views
0

Lorsque j'ajoute une commande ApplicationCommands à un MenuItem dans mon menu fichier, peu importe via XAML ou via le code, lorsque j'ouvre le menu, l'application se bloque dans un débordement de pile, sans aucun des détails sur le problème. Lorsque je supprime la commande, le problème disparaît également. Peu importe quelle ApplicationCommand j'utilise.Dépassement de pile lors de l'ouverture du menu avec ApplicationCommands en C#

Une partie de la pile d'appel:

  • WindowsBase.dll MS.Utility.ArrayItemList.ArrayItemList (int taille ) + 0x20 octets
  • WindowsBase.dll MS.Utility.FrugalStructList.Capacity!. set (int valeur) + 0x6A octets
  • WindowsBase.dll! MS.Utility.FrugalStructList.FrugalStructList (int taille) + 0x9 octets
  • PresentationCore.dll! System.Windows.EventRoute.EventRoute (System.Windows. Ro utedEvent RoutedEvent) + 0x35 octets
  • PresentationCore.dll! System.Windows.EventRouteFactory.FetchObject (System.Windows.RoutedEvent RoutedEvent) + 0x31 octets
  • PresentationCore.dll! System.Windows.UIElement.RaiseEventImpl (système. Windows.DependencyObject expéditeur = { System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = { System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x3f octets
  • PresentationCore.dll! System.Windows. UIElement.RaiseEvent (System.Windows.RoutedEventArgs args = {} System.Windows.Input.CanExecuteRoutedEventArgs, bool confiance) + 0x35 octets
  • PresentationCore.dll! System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper (objet paramètre , cible System.Windows.IInputElement, bool confiance , System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 octets
    PresentationCore.dll! System.Windows.Input.RoutedCommand.CanExecuteImpl (objet paramètre = nULL, cible System.Windows.IInputElement = {System.Windows .Controls.RichTextBox}, bool trust = false, out bool co ntinueRouting = false) + 0x70 octets
  • PresentationCore.dll! System.Windows.Input.RoutedCommand.CriticalCanExecute (objet paramètre , cible System.Windows.IInputElement, bool confiance, sur bool continueRouting) + 0x3a octets
  • PresentationCore.dll! System.Windows.Input.CommandManager.TransferEvent (System.Windows.IInputElement Newsource, System.Windows.Input.CanExecuteRoutedEventArgs e = { System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x52 octets
  • PrésentationCor e.dll! System.Windows.Input.CommandManager.OnCanExecute (sender objet , System.Windows.Input.CanExecuteRoutedEventArgs e) + 0x8c octets
    PresentationCore.dll! System.Windows.UIElement.OnCanExecuteThunk (objet expéditeur, System.Windows.Input.CanExecuteRoutedEventArgs e) + 0x44 octets
  • PresentationCore.dll! System.Windows.Input.CanExecuteRoutedEventArgs.InvokeEventHandler (System.Delegate GenericHandler, objet cible) + 0x41 octets
    PresentationCore.dll! System.Windows.RoutedEventArgs.InvokeHandler (gestionnaire System.Delegate , cible de l'objet) + 0x27 octets PresentationCore.dll! System.Windows.RoutedEventHandlerInfo.InvokeHandler (objectif de l'objet , System.Windows.RoutedEventArgs RoutedEventArgs) + 0x3E octets
    système PresentationCore.dll!. Windows.EventRoute.InvokeHandlersImpl (objet source = { System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = { System.Windows.Input.CanExecuteRoutedEventArgs}, bool = false relance) + 0x1bf octets
  • PresentationCore.dll système! .Windows.UIElement.RaiseEventImpl (System.Windows.DependencyObject expéditeur = { System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = + 0x79 octets
  • PresentationCore.dll! System.Windows.UIElement. RaiseEvent (System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool confiance) + 0x35 octets
  • PresentationCore.dll! System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper (objet paramètre, cible System.Windows.IInputElement, bool confiance, System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 octets

Il semble que l'application soit bloquée dans une boucle sans fin. Est-ce ma faute (et qu'est-ce que je fais mal) ou un bug dans .NET 3.5?

J'utilise ce code:

MenuItem mi = new MenuItem(); 
mi.Command = ApplicationCommands.Open; 
FileMenu.Items.Add(mi); 

Peu importe wheter je crée le ligneMenu par code ou en XAML, et comme je l'ai dit le réglage de la commande n'a pas d'importance où. Le problème se produit également lors de l'utilisation de MediaCommands, donc je suppose que pour toutes les commandes en général.

Le code RichTextBox:

//configure richtextbox 
sb = new RichTextBox(); 
sb.Margin = new Thickness(-3); 
sb.BorderThickness = new Thickness(0); 
sb.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; 
sb.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; 
///TODO: get font from preferences. 
FontFamilyConverter ffc = new FontFamilyConverter(); 
sb.FontFamily = (FontFamily)ffc.ConvertFromString("Lucida Sans Unicode"); 
sb.FontSize = 13; 
sb.AcceptsReturn = true; sb.AcceptsTab = true; 
sb.AllowDrop = true; sb.IsDocumentEnabled = false; 
sb.Padding = new Thickness(5); 

//markup styles 
Style s = new Style(typeof(Paragraph)); 
s.Setters.Add(new Setter(Paragraph.MarginProperty, new Thickness(0))); 
sb.Resources.Add(typeof(Paragraph), s); 

this.AddChild(sb); 

Le RichTextBox est ajouté dans le constructeur d'une commande qui est dérivé de TabItem.

+0

Où se trouve ce code? Quelle est la méthode? – Yohnny

+0

Le code est dans le constructeur de la fenêtre. Ce n'est pas grave si je mets la commande avant que le MenuItem soit ajouté au Menu ou après, j'ai essayé les deux. – Ruud

+0

Pour moi, il est étrange de voir System.Windows.Controls.RichTextBox comme expéditeur dans cette trace de la pile. –

Répondre

0

J'ai trouvé le problème. J'ajoutais mon RichTextBox à un groupe de discussion. J'ai enlevé ça et maintenant ça marche. Bien que je ne peux toujours pas obtenir le focus du clavier dans la RichTextBox au démarrage. (Essayé Keyboard.Focus (sb), sb.Focus(), sb.Document.Focus(), Keyboard.Focus (sb.Document), FocusManager.SetFocusedElement (this, sb), ect ...

0

Oui, c'est de votre faute et pas un bug dans 3.5 (hé, vous avez demandé). Maintenant, pour trouver votre bogue ...

De la trace de la pile, il y a beaucoup d'événements CanExecute, et ils semblent être associés à un RichTextBox. Avez-vous une logique CanExecute dans votre code? Honnêtement, nous avons besoin de plus de code pour aider efficacement.

De plus, une boucle sans fin et un dépassement de pile sont différents. Votre programme a-t-il déjà explosé avec l'exception SO ou continue-t-il de fonctionner pour toujours?

+0

Non, juste l'erreur StackOverflow. Je n'ai aucune logique CanExecute dans mon code. (J'ai ajouté le code RichTextbox à ma question). – Ruud

+0

Vous devez avoir une sorte d'appel récursif/circulaire là-dedans. Avez-vous des gestionnaires d'événements qui font une logique qu'ils ne devraient pas être? –

+0

J'ai fait un autre test, et il s'avère que toutes les commandes ne retournent pas d'erreur ... ApplicationCommands.Undo doens't. – Ruud

Questions connexes