使用C#中的ApplicationCommands打開菜單時堆棧溢出

[英]Stack overflow when opening menu with ApplicationCommands in C#


When I add an ApplicationCommands command to a MenuItem in my file menu, no matter via XAML or via code, when I open the menu the application crashes in a stack overflow, with absolutely no details about the problem. When I remove the Command, the problem also disappears. It doesn't matter which ApplicationCommand I use.

當我在文件菜單中將MenuCommands命令添加到MenuItem時,無論是通過XAML還是通過代碼,當我打開菜單時,應用程序在堆棧溢出時崩潰,絕對沒有關於問題的詳細信息。當我刪除命令時,問題也消失了。我使用哪個ApplicationCommand並不重要。

Part of the call stack:

調用堆棧的一部分:

  • WindowsBase.dll!MS.Utility.ArrayItemList.ArrayItemList(int size) + 0x20 bytes
  • WindowsBase.dll!MS.Utility.ArrayItemList.ArrayItemList(int size)+ 0x20 bytes

  • WindowsBase.dll!MS.Utility.FrugalStructList.Capacity.set(int value) + 0x6a bytes
  • WindowsBase.dll!MS.Utility.FrugalStructList.Capacity.set(int value)+ 0x6a bytes

  • WindowsBase.dll!MS.Utility.FrugalStructList.FrugalStructList(int size) + 0x9 bytes
  • WindowsBase.dll!MS.Utility.FrugalStructList.FrugalStructList(int size)+ 0x9 bytes

  • PresentationCore.dll!System.Windows.EventRoute.EventRoute(System.Windows.RoutedEvent routedEvent) + 0x35 bytes
  • PresentationCore.dll!System.Windows.EventRoute.EventRoute(System.Windows.RoutedEvent routedEvent)+ 0x35 bytes

  • PresentationCore.dll!System.Windows.EventRouteFactory.FetchObject(System.Windows.RoutedEvent routedEvent) + 0x31 bytes
  • PresentationCore.dll!System.Windows.EventRouteFactory.FetchObject(System.Windows.RoutedEvent routedEvent)+ 0x31 bytes

  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x3f bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.RichTextBox},System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs})+ 0x3f bytes

  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool trusted) + 0x35 bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs},bool trusted)+ 0x35 bytes

  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(object parameter, System.Windows.IInputElement target, bool trusted, System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 bytes
    PresentationCore.dll!System.Windows.Input.RoutedCommand.CanExecuteImpl(object parameter = null, System.Windows.IInputElement target = {System.Windows.Controls.RichTextBox}, bool trusted = false, out bool continueRouting = false) + 0x70 bytes
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(object parameter,System.Windows.IInputElement target,bool trusted,System.Windows.Input.CanExecuteRoutedEventArgs args)+ 0x80 bytes PresentationCore.dll!System.Windows.Input.RoutedCommand .CanExecuteImpl(object parameter = null,System.Windows.IInputElement target = {System.Windows.Controls.RichTextBox},bool trusted = false,out bool continueRouting = false)+ 0x70 bytes

  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecute(object parameter, System.Windows.IInputElement target, bool trusted, out bool continueRouting) + 0x3a bytes
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecute(對象參數,System.Windows.IInputElement目標,bool trusted,out bool continueRouting)+ 0x3a bytes

  • PresentationCore.dll!System.Windows.Input.CommandManager.TransferEvent(System.Windows.IInputElement newSource, System.Windows.Input.CanExecuteRoutedEventArgs e = {System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x52 bytes
  • PresentationCore.dll!System.Windows.Input.CommandManager.TransferEvent(System.Windows.IInputElement newSource,System.Windows.Input.CanExecuteRoutedEventArgs e = {System.Windows.Input.CanExecuteRoutedEventArgs})+ 0x52 bytes

  • PresentationCore.dll!System.Windows.Input.CommandManager.OnCanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e) + 0x8c bytes
    PresentationCore.dll!System.Windows.UIElement.OnCanExecuteThunk(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e) + 0x44 bytes
  • PresentationCore.dll!System.Windows.Input.CommandManager.OnCanExecute(object sender,System.Windows.Input.CanExecuteRoutedEventArgs e)+ 0x8c bytes PresentationCore.dll!System.Windows.UIElement.OnCanExecuteThunk(object sender,System.Windows.Input。 CanExecuteRoutedEventArgs e)+ 0x44字節

  • PresentationCore.dll!System.Windows.Input.CanExecuteRoutedEventArgs.InvokeEventHandler(System.Delegate genericHandler, object target) + 0x41 bytes
    PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) + 0x27 bytes PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) + 0x3e bytes
    PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool reRaised = false) + 0x1bf bytes
  • PresentationCore.dll!System.Windows.Input.CanExecuteRoutedEventArgs.InvokeEventHandler(System.Delegate genericHandler,object target)+ 0x41 bytes PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler,object target)+ 0x27 bytes PresentationCore。 dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target,System.Windows.RoutedEventArgs routedEventArgs)+ 0x3e bytes PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.RichTextBox},System。 Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs},bool reRaised = false)+ 0x1bf bytes

  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = + 0x79 bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.RichTextBox},System.Windows.RoutedEventArgs args = + 0x79 bytes

  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool trusted) + 0x35 bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs},bool trusted)+ 0x35 bytes

  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(object parameter, System.Windows.IInputElement target, bool trusted, System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 bytes
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(object parameter,System.Windows.IInputElement target,bool trusted,System.Windows.Input.CanExecuteRoutedEventArgs args)+ 0x80 bytes

It looks like the application is stuck in an endless loop. Is this my fault (and what am I doing wrong) or a bug in .NET 3.5?

看起來應用程序陷入無限循環。這是我的錯(我做錯了什么)或.NET 3.5中的錯誤?

I use this code:

我用這個代碼:

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

It doesn't matter wheter I create the menuItem via code or in XAML, and like I said setting the Command also doesn't matter where. The problem also occurs when using MediaCommands, so I guess for all Commands in general.

我通過代碼或XAML創建menuItem並不重要,就像我說設置命令也無所謂。使用MediaCommands時也會出現問題,所以我想一般所有的命令。

The RichTextBox 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);

The RichTextBox is added in the constructor of a control that is derived from TabItem.

RichTextBox添加在從TabItem派生的控件的構造函數中。

2 个解决方案

#1


Yes, this is your fault and not a bug in 3.5 (hey, you asked). Now to find your bug...

是的,這是你的錯,而不是3.5中的錯誤(嘿,你問過)。現在找到你的bug ...

From the stack trace, there are a lot of CanExecute events, and they seem to be associated with a RichTextBox. Do you have any CanExecute logic in your code? Honestly, we need more code in order to help effectively.

從堆棧跟蹤中,有許多CanExecute事件,它們似乎與RichTextBox相關聯。您的代碼中是否有任何CanExecute邏輯?老實說,我們需要更多代碼才能有效地提供幫助。

Also, an endless loop and stack overflow are different. Does your program ever blow up with the SO exception or does it just keep running forever?

此外,無限循環和堆棧溢出是不同的。您的程序是否會因SO異常而爆炸,或者它是否會一直運行?

#2


I found the problem. I was adding my RichTextBox to a focus group. I removed that and now it works. Altough I still can't get the keyboard focus in the RichTextBox at startup. (Tried Keyboard.Focus(sb), sb.Focus(), sb.Document.Focus(), Keyboard.Focus(sb.Document), FocusManager.SetFocusedElement(this, sb), ect...

我發現了這個問題。我正在將RichTextBox添加到焦點組。我刪除了它,現在它可以工作。盡管如此,我仍然無法在啟動時將鍵盤焦點放在RichTextBox中。 (嘗試過Keyboard.Focus(sb),sb.Focus(),sb.Document.Focus(),Keyboard.Focus(sb.Document),FocusManager.SetFocusedElement(this,sb)等等...


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/07/09/725dfd91dc0e60b2b45aa0a7ea3643f7.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com