2
Vote

NullReferenceException when open XAML in Visual Studio 2010

description

After I convert this project to Visual Studio 2010, open the sample project WpfApplication1's Window1.xaml, VS2010 failed to open the XAML designer and showed exception as below:

System.NullReferenceException
Object reference not set to an instance of an object.
at dragonz.actb.core.AutoCompleteManager.AttachTextBox(TextBox textBox) in D:\2\AutoCompleteTextBox\dragonz.actb\core\AutoCompleteManager.cs:line 129

at dragonz.actb.control.AutoCompleteTextBox.AutoCompleteTextBox_Loaded(Object sender, RoutedEventArgs e) in D:\2\AutoCompleteTextBox\dragonz.actb\control\AutoCompleteTextBox.cs:line 29

This is because the following line in AutoCompleteManager.cs:

var ownerWindow = Window.GetWindow(_textBox);
if (ownerWindow.IsLoaded)
{
Initialize();
}

The ownerWindow is always NULL at design-time. It is no problem at run-time.
So my solution to this issue is to check design mode before hand:

if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(textBox))
{
return;
}

Works for me.

comments