Issues Initializing UserControl(s) at Design Time

by mheydt 13. April 2008 14:57 >
One of the things that I typically like to do when writing an application is to code one or more "Model" objects.  A model object is an object that represents a representation of domain objects and that provide a unified interface to those objects similar to what is provided by using the 'Facade" pattern.

I typically create the model object(s) in the constructor of the application, and then allow access to them through properties in the application object.  Since the application object is a singleton (yes, another pattern) any other object (specificailly those that are WPF framework objects) can access this singleton and also the domain objects of the application. 

The advantage of this is that it provides the ability to have a centralized place to access the objects instead of having to either spread them out all over the application, or to constantly have to serialize / deserialize them from persistent medium.

In an application that I am currently building I am doing this, and I have a user control (actually I have a few) that during initialization need to access the model object during their initialization to pre-populate their data (such as items in a listbox).  Matter of fact, this is actual code that I had that was doing just that:



This code works great at runtime, but it has issues at design time.  At design time, when editing a control, the designer actually instantiates an instance of the control to provide the designer with a visual representation and access to the properties of the control in the property editor.  However, at this time, since it is design and not run time, the App.Current object is NULL!  I guess that this makes sense, but it caused an exception to be thrown that renders an error message in the designer :-(.

The initial reaction I had to this when I figured it out was to change the code to be the following, which solves the problem quite handily:



However, this works well if you are working within your solution within VS.NET.  As this model has other issues, as described in this article.  The If statement will solve the problem within your own solution, but if the control is being used by another application at design time such as Blend, then the Application.Current object may not be your application object but Blends application object, and hence the TorrentModel property many not be found.

Second, although this code works fine within your own solution, I hate that I am now putting special code into the app to test for design time operation.  This then gets back to how can I remove this code and do this just in XAML?

Well, that will be the topic of a future post.

Tags:

.Net | Considered Dangerous | WPF

blog comments powered by Disqus

about the author

I'm a .NET, XAML, and iOS polyglot that loves playing with new things and making cool and innovative stuff.  I am also a Mac junkie.

I am Principal Technologist for SunGard Global Services in NYC, in their Advanced Technologies practice, and I work extensively with SunGard's energy and financial customers.

Note the the posting on this blog are my own and do not represent the position, strategies or opinions of SGS.

twitter

I can't stop thinking big!
Sunday 1:08AM via WindowsLive
Just watched Moneyball. That's my pick for best movie this year.
Saturday 3:51PM via WindowsLive
@vincebelpiede: Report: Skype For Windows Phone Beta Imminent http://t.co/KYNjgg1L#mhtnd
Wednesday 8:39AM via Twitter for Mac
@mashable: Kinect Fusion Will Turn Gaming (and More) Into a 3D Fun House - http://t.co/Ihrq2fY2#mhtnd
Wednesday 8:39AM via Twitter for Mac
New Kinect SDK: http://t.co/57MvA5L5 #mhtnd
Wednesday 8:39AM via Twitter for Mac
Follow me on Twitter

recent comments

None

month list