我一直收到这个错误:
System.NullReferenceException was unhandled by user code
Message=[Arg_NullReferenceException]
Arguments:
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=5.0.60401.00&File=mscorlib.dll&Key=Arg_NullReferenceException
StackTrace:
at Jantire.DoHomeworkView.TextAlignment_combobox_SelectionChanged(Object sender, SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.NotifyCollectionReady()
at System.Windows.Controls.ItemsControl.NotifyAllItemsAdded(IntPtr nativeItemsControl)
InnerException:
代码:
private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//This next line is where error is at
if (TextAlignment_combobox.SelectedIndex == 0)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left);
}
if (TextAlignment_combobox.SelectedIndex == 1)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
}
if (TextAlignment_combobox.SelectedIndex == 2)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
}
}
使用XAML:
<ComboBox Width="128" x:Name="TextAlignment_combobox" SelectionChanged="TextAlignment_combobox_SelectionChanged" ToolTipService.ToolTip="Text Alignment">
<ComboBoxItem Name="LeftAlignment_comboboxitem" Content="Left Alignment" IsSelected="True"/>
<ComboBoxItem Name="CenterAlignment_comboboxitem" Content="Center Alignment"/>
<ComboBoxItem Name="RightAlignment_comboboxitem" Content="Right Alignment"/>
</ComboBox>
答案 0 :(得分:5)
好的,我已经测试了这个场景,我找到了你的问题。当您最初触发WPF应用程序时,它将运行SelectionChanged事件。只要创建了ComboBox对象,就会发生这种情况。问题是,在您的WPF应用程序中,您在RichTextBox之前的XAML中有ComboBox。这意味着此事件在创建RichTextBox之前触发。因此,您将获得Null引用异常。你有两个选择。在尝试对其进行操作之前,您可以吃错误或尝试确定RichTextBox是否存在,或者您可以将XAML中的RichTextBox向上移动到ComboBox上方。这与表单放置无关,而是与XAML中的位置无关。任何一个都可以解决您的问题。
答案 1 :(得分:0)
没有更多细节,听起来像EssayContents_richtextbox为空。
答案 2 :(得分:0)
或者:
TextAlignment_combobox
为空(似乎非常不可能)EssayContents_richtextbox
为空EssayContents_richtextbox.Selection
为空你应该调试它并检查所有内容,直到找到null为空的东西,或者以某种方式阻止它们在代码中为null,例如:
private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (EssayContents_richtextbox == null || EssayContents_richtextbox.Selection == null)
{
// Handle me, or just
return;
}
if (TextAlignment_combobox.SelectedIndex == 0)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left);
}
if (TextAlignment_combobox.SelectedIndex == 1)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
}
if (TextAlignment_combobox.SelectedIndex == 2)
{
EssayContents_richtextbox.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
}
}
我猜如果EssayContents_richtextbox
为空,这是代码中的错误 - 同时查看Selection
属性的文档,看起来这可能会有一个值,即使有没有选择(尽管它没有明确表示它永远不会返回null)