【发布时间】: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