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 を使用しており、TreeView 内で現在選択されている項目を使用して、ViewModel で Command を呼び出したいと考えています。EventToCommand 動作を追加しようとするまで、これはすべて正常に機能していました。

ブロック内およびブロックを含むすべてを削除すると<i:Interaction.Triggers>、NullReferenceException が発生しなくなりますが、アイテムを選択しても動作しません。

明確にするために、エラーは、NullReferenceException を含む TargetInvocationException を含む XamlParseException として現れています。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
4

1 に答える 1

7

... 私はばかです。ここに投稿したスタック トレースを適切に読んだ後、問題はおそらく xmlns 宣言にあることに気付きました。案の定、プロジェクトへの参照としてアセンブリを再追加していませんでした。

私は質問を削除したくなりましたが、前回WPFで遊んだとき(1年以上前)に似たようなことをしたことがあります.私の愚かさが他の人の助けになることを願っています.

<i:暴言>

ちなみに、このような場合に生成される例外はひどいものです。「名前空間 'i' を読み込めませんでした。参照がありませんか?」という例外を発生させるのは本当に難しいでしょうか? <i:...>名前空間を使用したインテリセンスさえありました。

</i:暴言>

于 2011-04-20T12:28:53.550 に答える