WPF 4.0 InvokeCommandAction 兼容性 (System.Windows.Interactivity)

标签 wpf c#-4.0 expression-blend

在 WPF 4.0 中插入 InvokeCommandAction (Blend 4 SDK) 时遇到问题。用户控件未显示,并且我遇到了一些异常(exception)情况。 (登录底部) 如果我的 xaml 代码中没有该行,就没有问题(但没有事件触发器)。

我已经在外部灯光项目中测试了这部分代码,它工作完美! 也许 InvokeCommandAction 与可能的应用程序中的某些内容不兼容,但是什么呢?

<UserControl x:Class="Project.Views.Menu.TestView"
             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:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
             xmlns:Menu="clr-namespace:Project.ViewModels.Menu" 
             mc:Ignorable="d" 
             x:Name="test"
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.DataContext>
        <Menu:TestViewModel />
    </UserControl.DataContext>
    <Grid>
        <ListBox x:Name="listBox" Width="200" 
                    MaxHeight="128" SelectionMode="Single" Margin="0">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding TestCommand, Mode=OneWay}" CommandParameter="{Binding SelectedItem, ElementName=listBox}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>    
            <ListBoxItem>blabla1</ListBoxItem>
            <ListBoxItem>blabla2</ListBoxItem>
            <ListBoxItem>blabla3</ListBoxItem>
            <ListBoxItem>blabla4</ListBoxItem>
        </ListBox>
    </Grid>
</UserControl>
<小时/>

我已记录该错误,但我不明白:


ERROR 2011-07-26 15:18:47 – La référence d'objet n'est pas définie à une instance d'un objet.
PresentationFramework
   à System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   à System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   à System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   à System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   à System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   à Project.Views.Menu.TestView.InitializeComponent() dans C:\Project\Views\Menu\TestView.xaml:ligne 1
   à Project.Views.Menu.TestView..ctor() dans C:\Project\Views\Menu\TestView.xaml.cs:ligne 24
   à Project.ViewModels.MainWindowsViewModel..ctor() dans C:\Project\ViewModels\MainWindowsViewModel.cs:ligne 52

ERROR 2011-07-26 15:18:47 – Une exception a été levée par la cible d'un appel.
mscorlib
   à System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   à System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   à System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   à System.Activator.CreateInstance(Type type, Boolean nonPublic)
   à System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   à System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   à System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments)
   à MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args)
   à MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
   à System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
   à System.Xaml.XamlObjectWriter.WriteEndObject()
   à System.Xaml.XamlWriter.WriteNode(XamlReader reader)
   à System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   à System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   à System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   à System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   à System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   à Project.MainWindow.InitializeComponent() dans C:\Project\MainWindow.xaml:ligne 1
   à Project.MainWindow..ctor() dans C:\Project\MainWindow.xaml.cs:ligne 17


ERROR 2011-07-26 15:18:47 – Une exception a été levée par la cible d'un appel.
mscorlib
   à System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   à System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   à System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   à System.Activator.CreateInstance(Type type, Boolean nonPublic)
   à System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   à System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   à System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments)
   à MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args)
   à MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
   à System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
   à System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
   à System.Xaml.XamlWriter.WriteNode(XamlReader reader)
   à System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   à System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   à System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   à System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   à System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
   à System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
   à System.Windows.Application.DoStartup()
   à System.Windows.Application.b__1(Object unused)
   à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

最佳答案

您的 InteractionTrigger 应该看起来更像这样:

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding DataContext.TestCommand, ElementName=test}" CommandParameter="{Binding SelectedItem, ElementName=listBox}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers> 

希望我还不算太晚=)

关于WPF 4.0 InvokeCommandAction 兼容性 (System.Windows.Interactivity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6831025/

相关文章:

wpf - 如何隐藏 WPF DataGrid 中的列标题?

默认按钮上的 WPF 按钮内容 VerticalAlignment

wpf - ListBoxItem IsSelected 样式

使用 HTMLHelper 和 "All"选择下拉列表值的 JavaScript 在 ASP .NET MVC4 中不起作用

silverlight - 什么是 d :IsOptimized in Expressions Blend?

silverlight - 具有多个 <Run> 间距的 TextBlock

c# - 如何单击嵌套在数据网格内的数据网格中的复选框

wpf - 世界粮食计划署制图工具包 : show data point values above all columns

c#-4.0 - 如何使用 libphonenumber 以国际格式格式化电话号码。

正则表达式匹配反斜杠星