首先,代码:
<UserControl x:Class="Engage.IWS.Modules.InteractionResults.Views.InteractionResultView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
... more here that should be irrelevant ...
<TreeView
x:Name="lstResults"
Grid.Row="1"
ItemsSource="{Binding Children}"
>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate
ItemsSource="{Binding Children}"
DataType="{x:Type Models:InteractionResult}"
>
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{Binding Name}"
/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<cmd:EventToCommand Command="{Binding ResultSelected, Mode=OneWay}"
CommandParameter="{Binding ElementName=lstResults, Path=SelectedValue}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
我正在使用MvvmLight,我想在ViewModel上使用TreeView中当前选定的项目调用Command。这一切都正常,直到我尝试添加EventToCommand行为。
删除<i:Interaction.Triggers>
块中的所有内容可以防止发生NullReferenceException,但是在选择项目时我没有任何行为。
要清楚,错误表现为XamlParseException,它包含一个TargetInvocationException,其中包含NullReferenceException。这是我第一次尝试使用EventToCommand。
此异常的可能原因是什么,我该如何解决?
NullReferenceException堆栈跟踪是:
at System.Windows.Markup.ReflectionHelper.IsPublicType(Type type)
at System.Windows.Markup.XamlTypeMapper.UpdateAttachedPropertyMethdodInfo(BamlAttributeInfoRecord attributeInfo, Boolean isSetter)
at System.Windows.Markup.XamlTypeMapper.UpdateAttachedPropertyGetter(BamlAttributeInfoRecord attributeInfo)
at System.Windows.Markup.PropertyDefinition.get_AttachedPropertyGetter()
at System.Windows.Markup.BamlCollectionHolder.InitDefaultValue()
at System.Windows.Markup.BamlCollectionHolder..ctor(BamlRecordReader reader, Object parent, Int16 attributeId, Boolean needDefault)
at System.Windows.Markup.BamlRecordReader.ReadPropertyIListStartRecord(BamlPropertyIListStartRecord bamlPropertyIListStartRecord)
at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)
at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()
at System.Windows.Markup.TreeBuilder.Parse()
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Engage.IWS.Modules.InteractionResults.Views.InteractionResultView.InitializeComponent() in c:\Users\admin\Documents\Visual Studio 2010\Projects\engage-iws-modules\Engage.IWS.Modules.InteractionResult\Views\InteractionResultView.xaml:line 1
at Engage.IWS.Modules.InteractionResults.Views.InteractionResultView..ctor(IInteractionResultViewModel viewModel) in C:\Users\admin\Documents\Visual Studio 2010\Projects\engage-iws-modules\Engage.IWS.Modules.InteractionResult\Views\InteractionResultView.xaml.cs:line 14
at Engage.IWS.Test.InteractionResults.FakeViewModel..ctor() in C:\Users\admin\Documents\Visual Studio 2010\Projects\engage-iws-modules\Engage.IWS.Modules.InteractionResult.Test.View\FakeViewModel.cs:line 17
at Engage.IWS.Test.InteractionResults.MainWindow..ctor() in C:\Users\admin\Documents\Visual Studio 2010\Projects\engage-iws-modules\Engage.IWS.Modules.InteractionResult.Test.View\MainWindow.xaml.cs:line 13
答案 0 :(得分:7)
......我是个白痴。在我读到我在这里发布的堆栈跟踪之后,正确地说,我意识到问题可能在xmlns声明中。果然,我没有重新添加程序集作为项目的引用。
我很想删除这个问题,但是上次我玩WPF之前我曾做过类似的事情(一年多前),希望我的愚蠢能帮助其他人。
&LT; I:咆哮&GT;
作为旁注,在这种情况下产生的例外是可怕的。提出一个说“命名空间'我无法加载,你错过了引用吗?”的异常真的很难吗?我甚至使用<i:...>
命名空间进行智能感知。
&LT; / I:咆哮&GT;