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

后端 未结 8 2231
一个人的身影
一个人的身影 2020-12-01 08:58

I\'m getting an \"Object reference not set to an instance of an object\" error when I try to reload the Designer for my XAML UserControl. Visual Studio highlig

相关标签:
8条回答
  • 2020-12-01 09:40

    It's probably something in the constructor of your user controls. VS2008 WPF designer appears have some issues with this.

    In a project we took over, we added:

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

    to the beginning of the constructor of the user controls where this happens to avoid that error.

    0 讨论(0)
  • 2020-12-01 09:45

    If you have 'Object reference not set to an instance of an object' in XAML, but your application compiles and runs fine, you will usually find out that its cause is something in a constructor that can't be resolved at design time.

    While you can find out the root of the problem with the help of other answers to this question, sometimes that is something you can't simply fix, you need it in your code exactly as you have it, but you don't want to see this error.

    In this case, just click the "Disable project code" button located on the bottom of your designer view and Visual Studio designer will stop trying to construct an instance to provide design time data view.

    See here for detailed information.

    0 讨论(0)
  • 2020-12-01 09:49

    What Alex says is the way to go. But I think its a little confusing to understand what he is saying.

    Assuming you have your project open in Visual Studio, open another Visual Studio instance and select Debug->Attach To Process. In the dialog which opens select

    • XDesProc.exe (which is the XAML UI Designer) for VS2012 and newer or
    • devenv.exe for older VS versions.

    Then do "Reload Designer" for the user control and see the output in the second VS instance to check what exactly is the error.

    0 讨论(0)
  • 2020-12-01 09:50

    If your user control throws exception at design time, you can debug it. To do this, open Dll project with this user control in Visual Studio. Select another Visual Studio instance as executable for debugging. Start debugging. In the second (debugged) Visual Studio instance use your user control in the client XAML page. By this way, you can debug user control in design mode.

    0 讨论(0)
  • 2020-12-01 09:54

    This thread is a little old, but I had a problem I just solved with its help, so I may be able to slightly clarify some points.

    1. Have your solution loaded in Visual Studio as usual.
    2. Open a 2nd instance of VS, menu debug/attach to process/select devenv. You should see nothing spectacular! In VS 2010, i just get "Disassembly cannot be displayed in run mode."
    3. Go back to your 1st instance, where your solution is opened. Load or reload the offending XAML file. If you have a problem (I had an exception on a user control, so I could not load that window), the debugger should point the offending code in the 2nd instance. in my case, it was very clear and obvious.

    To prevent the offending code from running at design time, I used

        If System.ComponentModel.LicenseUsageMode.Runtime = 1 Then
            myObject = New ObjectDefinition
        End If
    

    Works perfectly well.

    0 讨论(0)
  • 2020-12-01 09:55

    I was having this error today after editing a lot of XAML in my UWP code and I couldn't figure out what was wrong... but after some close inspection, I noticed this mistake I had made:

    <Button Click="{Binding MyCommand}" />
    

    I assigned my Command to the Click handler by mistake, and that resulted in a null reference exception... After changing Click to Command, the error went away.

    XAML error reporting needs to be improved!

    0 讨论(0)
提交回复
热议问题