How to avoid the exception "a reference to an object not installed on an instance of an object" in XAML code during development?

I have a problem with wpf user control, which is my own project. The problem is that I get an object reference not set to an instance of an object exception in the XAML code during development, when I implement usercontrol in my program.

How can I fix or suppress the output?

EDIT 1

The designer will show me the following information:

at Microsoft.Expression.Platform.InstanceBuilders.InstanceBuilderOperations.InstantiateType (Type type, Boolean supportInternal) at Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.Instantbectilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilionstilinstionuentioninstilioninstilioninstilioninstilioninstilioninstately (IInstanceBuilderContext context, ViewNode viewNode) in Microsoft.Expression.WpfPlatform.InstanceBuilders.FrameworkElementInstanceBuilder.Instantiate (IInstanceBuilderContext context, ViewNode viewNode) .InstanceInstance.nstanceInstanceUnntanceUnntanceInstanceUnntanceUnntanceInstanceUnstance.nstance.nstance.nstance.nstance.state Platform.InstanceBuilders.ViewNodeManager.CreateInstance (IInstanceBuilder builder, ViewNode viewNode)

I think they are not very useful ...

+8
source share
10 answers

If you have a link “Object link is not set to an instance of the object” in XAML, but your application compiles and works fine, you will usually find out that it is caused by something in the constructor that cannot be resolved with designing time.

You can simply click the “Disable Project Code” button located at the bottom of the design view, and the Visual Studio designer will stop trying to create an instance to provide a view of the development time data.

See here for more details and screenshots.

+8
source

Everything that happens in your constructor throws an exception during development. I had the same problem - I just made an attempt to catch the problem code - in my case, I called ServiceLocator.Current, because I use the IoC container. But during development there is no container. So I wrapped up in an attempt to catch in order to suppress the error, and it worked. Not the best solution ... but its solution.

+6
source

I tend to use the LicenseManager class in System.ComponentModel to avoid my ViewModels throwing nasty bugs during development. For instance:

 public MyViewModel() { if (LicenseManager.UsageMode == LicenseUsageMode.Runtime) { // Do runtime stuff } } 
+3
source

I had a similar problem. You just need to go to Tools> Options> XAML Designer and enable the option

Msgstr "Run project code in the XAML constructor."

Finally restart Visual Studio. I hope this helps.

+2
source

Tweaking the @BobHorn example, I got this to work for me:

 public class ViewModel { public ViewModel() { if (!IsInDesignMode) { //Constructor code here... } } public bool IsInDesignMode { get { var prop = DesignerProperties.IsInDesignModeProperty; return (bool)DependencyPropertyDescriptor .FromProperty(prop, typeof(FrameworkElement)) .Metadata.DefaultValue; } } } 

Although using its exact suggestion for the constructor

 public Main() { if (IsInDesignMode) { return; } //Constructor code here... } 

Also worked for me, I just prefer not to get confined in my methods with additional return statements. I would vote for his answer, I have no reputation yet.

+1
source

You can do something like this:

 using System.ComponentModel; using System.Windows; /// <summary> /// WPF Design Mode helper class. /// </summary> public static class DesignMode { private static bool? _isInDesignMode; /// <summary> /// Gets a value indicating whether the control is in design mode (running in Blend /// or Visual Studio). /// </summary> public static bool IsInDesignMode { get { if (!_isInDesignMode.HasValue) { var prop = DesignerProperties.IsInDesignModeProperty; _isInDesignMode = (bool)DependencyPropertyDescriptor .FromProperty(prop, typeof(FrameworkElement)) .Metadata.DefaultValue; } return _isInDesignMode.Value; } } } 

Then, as the first line in the constructor of your view (or view model), you can do something like this:

 if (DesignMode.IsInDesignMode) { return; } 

That way, your code will only run when it runs.

0
source

VS 2017 UWP:

 if (false == Windows.ApplicationModel.DesignMode.DesignModeEnabled) { // Anything in here need not be OK at Design time in Visual Studio } 
0
source

In the "partial class" of XAML, if you see " [XamlCompilation(XamlCompilationOptions.Compile)] ", just delete the line and then try to build again.

0
source

If someone else comes here, I accidentally drag my MainWindow.xaml file into a subfolder. Dragging back solved the problem.

0
source

When you work with WIndow / UserControl in the constructor, it "starts" the constructor without parameters. If you have code that depends on something usually provided by another piece of code, then this often causes problems. The constructor does not run any other code at first, so dependencies usually provided elsewhere may be missing and cause errors. To eliminate them, it is necessary to determine whether this code is executed in the constructor or not. It is often most convenient to simply return the constructor:

 public MainWindow() { InitializeComponent(); if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return; //code } 

Read more https://social.technet.microsoft.com/wiki/contents/articles/29874.aspx?Redirected=true

0
source

Source: https://habr.com/ru/post/949650/


All Articles