MvvmLight在TreeView上使用EventToCommand时抛出NullReferenceException异常

8

首先,是代码:

<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中当前选择的项目。这一切都很好地工作,直到我尝试添加EventToCommand行为。
删除包括块内的所有内容可以防止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
1个回答

7

我真是个傻瓜。在仔细阅读我发布的堆栈跟踪之后,我意识到问题可能出在xmlns声明中。果然,我没有将这些程序集重新添加到项目中作为引用。

我曾经在上一次使用WPF(一年前)时做过类似的事情,当时我想直接删除这个问题,但希望我的愚蠢能帮助其他人解决自己的问题。

<i:发泄>

顺便说一句,这种情况下生成的异常信息真是太糟糕了。难道不应该抛出一个异常,提示“无法加载'i'命名空间,请检查是否缺少引用?”吗?我甚至使用了 <i:...> 命名空间的智能感知功能。

</i:发泄>


你有检查内部异常吗?此外,装配加载异常和构造函数异常通常是最难调试的,因为这些异常提供的信息最少... - user1228
我遇到了同样的情况:) 我认为异常信息应该更加详细...并且根本没有InnerException... - 6opuc
Josh,你不是白痴,这个问题一点也不明显。谢谢你没有删除你的答案,它真的帮了我很多。 我想再补充一点: 如果你在某个类库中使用它,然后在另一个项目中引用它,而该项目没有对MVVM Light(或任何仅通过XAML使用的其他库)进行显式引用。那么这个库将不会被复制到目标目录中,你将遇到相同的问题或库加载失败。 因此,在这种情况下,您必须在主项目中显式引用依赖项。 - Vladimir Perevalov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接