I am trying to use the EventToCommand trigger from the MVVM Light Toolkit to execute a command on the closing event of the window, but I always get Object reference not set to an instance of an object when the window opens. This is a vs2008 WPF 3.5 project and I am referencing GalaSoft.MvvmLight, GalaSoft.MvvmLight.Extras, and System.Windows.Interactivity
Edit: my project is currently setup with this form being called from a WinForms form menu item. I was able to get past the exception by referencing GalaSoft.MvvmLight, GalaSoft.MvvmLight.Extras, and System.Windows.Interactivity in the WinForms assembly, but the close command is never executed.
My xaml look like the below:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras" x:Class="MyClass"
Title="{Binding DisplayName}" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" SizeToContent="WidthAndHeight">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=CloseCommand}" PassEventArgsToCommand="False" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Window>
And it is bound to the command that looks like this:
private ICommand _closeCommand;
public ICommand closeCommand
{
get
{
if(_closeCommand == null)
{
_closeCommand = new RelayCommand(param => Closing(), param => ClosingCanExecute);
}
return _closeCommand;
}
}
The details of the exception are below:
Object reference not set to an instance of an object.
ERROR : Object reference not set to an instance of an object. Type : NullReferenceException Location: Boolean IsPublicType(System.Type) Stack Trace: 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 MyProject.Views.MyClass.InitializeComponent() in n:\MyProject\Views\MyClass.xaml:line 1 at MyProject.Views.MyClass..ctor() in N:\MyProject\Views\MyClass.xaml.cs:line 31 at MyProject.MainForm.myClassToolStripMenuItem_Click(Object sender, EventArgs e) in N:\MyProject\MainForm.cs:line 372 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at Myproject.Program.Run() in N:\MyProject\Program.cs:line 82