在我向 WPF 4.0 中插入 InvokeCommandAction(Blend 4 SDK)时遇到了问题,用户控件未显示并且出现了一些异常。 (底部日志) 如果没有 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 – Object reference not set to an instance of an object. PresentationFramework at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector) at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at Project.Views.Menu.TestView.InitializeComponent() in C:\Project\Views\Menu\TestView.xaml:line 1 at Project.Views.Menu.TestView..ctor() in C:\Project\Views\Menu\TestView.xaml.cs:line 24 at Project.ViewModels.MainWindowsViewModel..ctor() in C:\Project\ViewModels\MainWindowsViewModel.cs:line 52ERROR 2011-07-26 15:18:47 – An exception was thrown by the target of an invocation. mscorlib at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args) at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx) at System.Xaml.XamlObjectWriter.WriteEndObject() at System.Xaml.XamlWriter.WriteNode(XamlReader reader) at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector) at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at Project.MainWindow.InitializeComponent() in C:\Project\MainWindow.xaml:line 1 at Project.MainWindow..ctor() in C:\Project\MainWindow.xaml.cs:line 17
ERROR 2011-07-26 15:18:47 – An exception was thrown by the target of an invocation. mscorlib at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean non