locked
Which events are executed by designer? RRS feed

  • Question

  • Hello

    I have some operations that I executed, until now, in the constructor of some self-made usercontrols. Now some require another assembly and build up some rows according to a database etc.

    The thing is, if that code is in my constructor - the designer fails because something may be NULL. So I thought I would fix the problem by moving this code into the Loaded event. Same thing!

    Can anyone tell me, what Code the Visual Studio XAML Designer executes? As some of my things are NULL it means not everything is executed but some parts are. I don't really wanna go and put IFs in my code to test if it's in design mode or not - I'd rather understand what it does.

    Thanks

    ~N

    Monday, May 17, 2010 12:10 PM

Answers

  • While you are designing your UserControl non of your code is executed. However, when you place an instance of your UserControl onto another form then, just lie at runtime, the default constructor, Loaded event, etc. get executed. The main difference is that the Application object is not what you think it is. At designtime it's simply an empty Application object, and that causes many load failures because of things not being initialized there like they would at runtime.

    The official guidance is to put those IFs into your code to avoid running unnecessary code at designtime. By attaching another instance of VS and breaking on NullRef exceptions you can usually figure out pretty quickly what was uninitialized and needs to be prevented from running.

    I hope this helps.


    Marco Goertz \ Sr. Dev Lead \ WPF/SL Designer "Cider" \ Microsoft
    • Marked as answer by NoRyb Monday, May 31, 2010 6:15 AM
    Monday, May 17, 2010 11:59 PM
  • Then I would suggest you attach the debugger of another instance of VS and set a breakpoint in the constructor of MyClass and see what's actually going on (or not going on). That's the best, and most of the time only, way to deal with strange issues like this.
    Marco Goertz \ Sr. Dev Lead \ WPF/SL Designer "Cider" \ Microsoft
    • Marked as answer by NoRyb Monday, May 31, 2010 6:15 AM
    Wednesday, May 26, 2010 3:26 PM

All replies

  • While you are designing your UserControl non of your code is executed. However, when you place an instance of your UserControl onto another form then, just lie at runtime, the default constructor, Loaded event, etc. get executed. The main difference is that the Application object is not what you think it is. At designtime it's simply an empty Application object, and that causes many load failures because of things not being initialized there like they would at runtime.

    The official guidance is to put those IFs into your code to avoid running unnecessary code at designtime. By attaching another instance of VS and breaking on NullRef exceptions you can usually figure out pretty quickly what was uninitialized and needs to be prevented from running.

    I hope this helps.


    Marco Goertz \ Sr. Dev Lead \ WPF/SL Designer "Cider" \ Microsoft
    • Marked as answer by NoRyb Monday, May 31, 2010 6:15 AM
    Monday, May 17, 2010 11:59 PM
  • Okay so I will have to put those IFs. Just out of curiousity, in the default constructor of my UserControl I create an Instance of a Class (let's call it "myInstance") which - in it's constructor - creates another instance and saves it in a Property called "AProperty". The designer reports a NullReference Exception when I use the following code in the constructor of my UserControl:

    MyClass myInstance = new MyClass();
    myInstance.AProperty.DoSomething();

    Is the code only executed to a certain depth when executed in design mode?

    Thank you

    Tuesday, May 18, 2010 6:46 AM
  • Isn't there a documentation about that?
    Tuesday, May 25, 2010 7:31 AM
  • Not that I'm aware of. This is just how it works internally.

    For the NullRef it would be helpful to see the callstack in the designer's exception pane.


    Marco Goertz \ Sr. Dev Lead \ WPF/SL Designer "Cider" \ Microsoft
    Tuesday, May 25, 2010 2:53 PM
  • It only tells me the line in my constructor on which the Property of the example I gave you before is NULL.
    Wednesday, May 26, 2010 6:48 AM
  • Then I would suggest you attach the debugger of another instance of VS and set a breakpoint in the constructor of MyClass and see what's actually going on (or not going on). That's the best, and most of the time only, way to deal with strange issues like this.
    Marco Goertz \ Sr. Dev Lead \ WPF/SL Designer "Cider" \ Microsoft
    • Marked as answer by NoRyb Monday, May 31, 2010 6:15 AM
    Wednesday, May 26, 2010 3:26 PM
  • Okay thanks.
    Monday, May 31, 2010 6:14 AM