使用C#中的ApplicationCommands打开菜单时堆栈溢出

时间:2009-07-09 20:08:22

标签: c# wpf stack-overflow command

当我在文件菜单中向MenuItem添加ApplicationCommands命令时,无论是通过XAML还是通过代码,当我打开菜单时,应用程序在堆栈溢出时崩溃,绝对没有关于问题的详细信息。当我删除命令时,问题也消失了。使用哪个ApplicationCommand并不重要。

调用堆栈的一部分:

  • WindowsBase.dll中!MS.Utility.ArrayItemList.ArrayItemList(INT 大小)+ 0x20字节
  • WindowsBase.dll中!MS.Utility.FrugalStructList.Capacity.set(INT 值)+ 0x6a字节
  • WindowsBase.dll中!MS.Utility.FrugalStructList.FrugalStructList(INT 大小)+ 0x9字节
  • PresentationCore.dll中!System.Windows.EventRoute.EventRoute(System.Windows.RoutedEvent routedEvent)+ 0x35 bytes
  • PresentationCore.dll中!System.Windows.EventRouteFactory.FetchObject(System.Windows.RoutedEvent routedEvent)+ 0x31字节
  • 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.Input.RoutedCommand.CriticalCanExecuteWrapper(对象 参数, System.Windows.IInputElement目标, 布尔信任, System.Windows.Input.CanExecuteRoutedEventArgs args)+ 0x80字节
    PresentationCore.dll中!System.Windows.Input.RoutedCommand.CanExecuteImpl(对象 参数= 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(对象 参数, System.Windows.IInputElement目标, 布尔信任,布尔 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.OnCanExecute(对象 发件人, System.Windows.Input.CanExecuteRoutedEventArgs e)+ 0x8c字节
    PresentationCore.dll中!System.Windows.UIElement.OnCanExecuteThunk(对象 发件人, System.Windows.Input.CanExecuteRoutedEventArgs e)+ 0x44字节
  • PresentationCore.dll中!System.Windows.Input.CanExecuteRoutedEventArgs.InvokeEventHandler(System.Delegate genericHandler,object target)+ 0x41 字节
    PresentationCore.dll中!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate 处理程序,对象目标)+ 0x27字节 PresentationCore.dll中!System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 目标, System.Windows.RoutedEventArgs routedEventArgs)+ 0x3e个字节
    PresentationCore.dll中!System.Windows.EventRoute.InvokeHandlersImpl(对象 来源= {} 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.RaiseEvent(System.Windows.RoutedEventArgs args = {} System.Windows.Input.CanExecuteRoutedEventArgs, bool trusted)+ 0x35 bytes
  • PresentationCore.dll中!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(对象 参数, System.Windows.IInputElement目标, 布尔信任, System.Windows.Input.CanExecuteRoutedEventArgs args)+ 0x80 bytes

看起来应用程序陷入无限循环。这是我的错(我做错了什么)或.NET 3.5中的错误?

我使用此代码:

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

通过代码或XAML创建menuItem并不重要,就像我说设置命令也无所谓。使用MediaCommands时也会出现问题,所以我猜想一般所有的命令。

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

RichTextBox添加在从TabItem派生的控件的构造函数中。

2 个答案:

答案 0 :(得分:0)

是的,这是你的错,而不是3.5中的错误(嘿,你问过)。现在找到你的错误...

从堆栈跟踪中,有很多CanExecute事件,它们似乎与RichTextBox相关联。您的代码中是否有任何CanExecute逻辑?老实说,我们需要更多代码才能有效地提供帮助。

此外,无限循环和堆栈溢出是不同的。您的程序是否会因SO异常而爆炸,或者它是否会永远运行?

答案 1 :(得分:0)

我发现了问题。我正在将RichTextBox添加到焦点组。我删除了它,现在它可以工作。尽管如此,我仍然无法在启动时将键盘焦点放在RichTextBox中。 (尝试过Keyboard.Focus(sb),sb.Focus(),sb.Document.Focus(),Keyboard.Focus(sb.Document),FocusManager.SetFocusedElement(this,sb),等...