【问题标题】:Stack overflow when opening menu with ApplicationCommands in C#在 C# 中使用 ApplicationCommands 打开菜单时堆栈溢出
【发布时间】:2010-11-09 12:32:01
【问题描述】:

当我将 ApplicationCommands 命令添加到文件菜单中的 MenuItem 时,无论是通过 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 字节
  • PresentationCore.dll!System.Windows.EventRouteFactory.FetchObject(System.Windows.RoutedEvent routedEvent) + 0x31 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject 发件人 = {System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs 参数 = {System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x3f 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs 参数 = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool 可信) + 0x35 字节
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(对象 范围, System.Windows.IInputElement 目标, 布尔信任, System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 字节
    PresentationCore.dll!System.Windows.Input.RoutedCommand.CanExecuteImpl(对象 参数=空, System.Windows.IInputElement 目标 = {System.Windows.Controls.RichTextBox}, bool 受信任 = false, out bool 继续路由=假)+ 0x70 字节
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecute(对象 范围, System.Windows.IInputElement 目标, bool 受信任,out bool continueRouting) + 0x3a 字节
  • PresentationCore.dll!System.Windows.Input.CommandManager.TransferEvent(System.Windows.IInputElement 新源, System.Windows.Input.CanExecuteRoutedEventArgs e = {System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x52 字节
  • 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,对象目标)+ 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 参数 = {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 参数 = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool 可信)+ 0x35 字节
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(对象 范围, System.Windows.IInputElement 目标, 布尔信任, System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 字节

看起来应用程序陷入了无限循环。这是我的错(我做错了什么)还是 .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 派生的控件的构造函数中。

【问题讨论】:

  • 这段代码在哪里?是用什么方法?
  • 代码在窗口的构造函数中。无论是在将 MenuItem 添加到菜单之前还是之后设置命令都没有关系,我都尝试了。
  • 对我来说,在堆栈跟踪中看到 System.Windows.Controls.RichTextBox 作为发件人很奇怪。
  • 我的窗口中确实有一个 RichTextBox,这有关系吗?

标签: c# wpf stack-overflow command


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 不,只是 StackOverflow 错误。我的代码中没有任何 CanExecute 逻辑。 (我将 RichTextbox 代码添加到我的问题中)。
    • 你必须在那里进行某种递归/循环调用。您是否有任何事件处理程序执行一些不应该执行的逻辑?
    • 我做了一些其他的测试,结果不是所有的命令都返回错误... ApplicationCommands.Undo 没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2011-03-02
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多