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

发布于 2024-07-27 16:49:58 字数 4715 浏览 11 评论 0原文

当我将 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.RoulatedEvent routedEvent) + 0x35 字节
  • PresentationCore.dll!System.Windows.EventRouteFactory.FetchObject(System.Windows.RoulatedEvent routedEvent) + 0x31 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject 发件人 = {系统.Windows.Controls.RichTextBox}, System.Windows.RoatedEventArgs args = {System.Windows.Input.CanExecuteRoulatedEventArgs}) + 0x3f 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoulatedEventArgs) 参数 = {System.Windows.Input.CanExecuteRoatedEventArgs}, bool 可信) + 0x35 字节
  • PresentationCore.dll!System.Windows.Input.RoulatedCommand.CriticalCanExecuteWrapper(对象 范围, System.Windows.IInputElement 目标, 布尔信任, System.Windows.Input.CanExecuteRoatedEventArgs args) + 0x80 字节
    PresentationCore.dll!System.Windows.Input.RoulatedCommand.CanExecuteImpl(对象 参数=空, System.Windows.IInputElement 目标 = {系统.Windows.Controls.RichTextBox}, bool 可信 = false,out bool 继续路由 = false) + 0x70 字节
  • PresentationCore.dll!System.Windows.Input.RoulatedCommand.CriticalCanExecute(对象 范围, System.Windows.IInputElement 目标, 布尔信任,输出布尔 continueRouting) + 0x3a 字节
  • PresentationCore.dll!System.Windows.Input.CommandManager.TransferEvent(System.Windows.IInputElement 新来源, System.Windows.Input.CanExecuteRoatedEventArgs e = {System.Windows.Input.CanExecuteRoulatedEventArgs}) + 0x52 字节
  • PresentationCore.dll!System.Windows.Input.CommandManager.OnCanExecute(object 发件人, System.Windows.Input.CanExecuteRoatedEventArgs e) + 0x8c 字节
    PresentationCore.dll!System.Windows.UIElement.OnCanExecuteThunk(对象 发件人, System.Windows.Input.CanExecuteRoatedEventArgs e) + 0x44 字节
  • PresentationCore.dll!System.Windows.Input.CanExecuteRoulatedEventArgs.InvokeEventHandler(System.Delegate genericHandler,对象目标)+ 0x41 字节
    PresentationCore.dll!System.Windows.RoulatedEventArgs.InvokeHandler(System.Delegate 处理程序、对象目标)+ 0x27 字节 PresentationCore.dll!System.Windows.RoulatedEventHandlerInfo.InvokeHandler(对象 目标, 系统.Windows.RoatedEventArgs routedEventArgs) + 0x3e 字节
    PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(对象 来源= {系统.Windows.Controls.RichTextBox}, System.Windows.RoatedEventArgs args = {System.Windows.Input.CanExecuteRoatedEventArgs}, bool reRaished = false) + 0x1bf 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject 发件人 = {系统.Windows.Controls.RichTextBox}, System.Windows.RoatedEventArgs args = + 0x79 字节
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoulatedEventArgs 参数= {System.Windows.Input.CanExecuteRoatedEventArgs}, bool 可信) + 0x35 字节
  • PresentationCore.dll!System.Windows.Input.RoulatedCommand.CriticalCanExecuteWrapper(对象 范围, System.Windows.IInputElement 目标, 布尔信任, System.Windows.Input.CanExecuteRoatedEventArgs args) + 0x80 字节

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

我使用这段代码:

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

无论是通过代码还是在 XAML 中创建 menuItem 都没关系,就像我说的在哪里设置 Command 也没关系。 使用 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 派生的控件的构造函数中。

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.

Part of the call stack:

  • 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 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.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.CriticalCanExecute(object
    parameter,
    System.Windows.IInputElement target,
    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.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.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.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

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?

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.

The RichTextBox code:

//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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-08-03 16:49:58

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

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

此外,无限循环和堆栈溢出是不同的。 你的程序是否曾经因 SO 异常而崩溃,或者只是永远运行?

Yes, this is your fault and not a bug in 3.5 (hey, you asked). Now to find your 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.

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?

心不设防 2024-08-03 16:49:58

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

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文