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