Question

I have already seen the below link

WPF, 'Object reference not set to an instance of an object' in Designer

My problem is similar but a little different. I am not setting any data context in XAML itself. I am creating my own user control and I am using that in another.

<usercontrols:MyControl1 x:Name="MyControl1"  Grid.Row="5"  Height="Auto" Width="Auto">

    </usercontrols:MyControl1>

This is the code I am using in second control to refer MyControl1. This generally does not throw any exception. But, When I specify some code in the constructer of MyControl1, the exception is coming. It also says "Cannot Create an Instance of MyControl1".

Était-ce utile?

La solution

Just add this line in MainPage.xaml.cs page

Use the static proeprty DesignerProperties.IsInDesignTool to determine if your code is currently being used in a designer tool.

InitializeComponent();
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
    //Code that throws the exception
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top