TreeView 上的 MvvmLight EventToCommand 抛出 NullReferenceException

发布于 2024-11-02 11:29:55 字数 4266 浏览 0 评论 0原文

首先是代码:

<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,并且我想使用 TreeView 中当前选定的项目来调用 ViewModel 上的命令。在我尝试添加 EventToCommand 行为之前,一切都工作正常。

删除 块内的所有内容(包括该块)可防止 NullReferenceException 发生,但在选择项目时我没有任何行为。

需要明确的是,该错误本身表现为 XamlParseException,其中包含 TargetInitationException,其中包含 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

First, the code:

<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>

I'm using MvvmLight, and I want to call a Command on the ViewModel with the currently selected item within the TreeView. This was all working fine until I attempted to add the EventToCommand behaviour.

Removing everything within, and including, the <i:Interaction.Triggers> block prevents the NullReferenceException from occurring, but then I don't have any behaviour upon selecting an item.

To be clear, the error is manifesting itself as an XamlParseException, which contains a TargetInvocationException, which contains the NullReferenceException. This is the first time I've tried to use EventToCommand.

What is the likely cause of this exception, and how do I fix it?

The NullReferenceException stack trace is:

   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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在梵高的星空下 2024-11-09 11:29:55

……我是个白痴。在我正确地阅读了我在此处发布的堆栈跟踪后,我意识到问题可能出在 xmlns 声明中。果然,我没有重新添加程序集作为对该项目的引用。

我很想删除这个问题,但我上次使用 WPF 时(一年多前)已经做过类似的事情,希望我的愚蠢能帮助其他人解决他们的问题。

<我:咆哮>

附带说明一下,在这种情况下生成的异常非常糟糕。引发“无法加载命名空间‘i’,您是否缺少引用?”的异常真的那么困难吗?我什至使用 命名空间进行智能感知。

... I am an idiot. After I read the stack trace I posted here, properly, I realised the problem was probably in the xmlns declaration. Sure enough, I hadn't re-added the assemblies as references to the project.

I was tempted to just delete the question, but I've done something similar before with WPF last time I played with it (over a year ago), and hopefully my stupidity will help someone else with their's.

<i:rant>

As a side note, the exceptions that are generated in such a case are terrible. Would it really be that difficult to raise an exception that said "Namespace 'i' could not be loaded, are you missing a reference?"? I even had intellisense using the <i:...> namespace.

</i:rant>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文