0

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

调用堆栈的一部分:

  • 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.FrugalStructList(int size) + 0x9 bytes
  • PresentationCore.dll!System.Windows.EventRoute.EventRoute(System.Windows.RoutedEvent routedEvent) + 0x35 字节
  • 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 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool trust) + 0x35 字节
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(对象参数,System.Windows.IInputElement 目标,bool 可信,System.Windows.Input.CanExecuteRoutedEventArgs 参数)+ 0x80 字节
    PresentationCore.dll!System.Windows.Input.RoutedCommand .CanExecuteImpl(object parameter = null, System.Windows.IInputElement target = {System.Windows.Controls.RichTextBox}, bool trust = false, out bool continueRouting = false) + 0x70 bytes
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecute(object parameter, System.Windows.IInputElement target, bool trust, 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 字节
  • 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 字节
    PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.RichTextBox}, System. Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs},bool reRaised = false) + 0x1bf 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject 发件人 = {System.Windows.Controls.RichTextBox},System.Windows.RoutedEventArgs args = + 0x79 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool trust) + 0x35 字节
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(对象参数,System.Windows.IInputElement 目标,bool 可信,System.Windows.Input.CanExecuteRoutedEventArgs 参数)+ 0x80 字节

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

我使用这段代码:

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

无论我是通过代码还是在 XAML 中创建 menuItem 都没有关系,就像我说的那样,在哪里设置命令也无关紧要。使用 MediaCommands 时也会出现这个问题,所以我猜一般是所有命令。

富文本框代码:

//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 派生的控件的构造函数中。

4

2 回答 2

0

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

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

此外,无限循环和堆栈溢出是不同的。您的程序是否曾经因 SO 异常而崩溃,或者它是否会一直运行下去?

于 2009-07-09T20:41:13.090 回答
0

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

于 2009-07-10T07:43:38.523 回答