Silverlight ResourceDictionary Issues

I have been developing WPF/Silverlight Applications about half an year already and I am charmed by the power of those technologies. But still there are some very strange and annoying issues I came upon, especially in my Silverlight implementations. The most favourite one is the one I will be explaining in this post. I am talking about the ResourceDictionary class.

Little intro

ResourceDictionary is a major class in the Silverlight platform, used to hold all the needed resources that you might create and use during your application lifecycle. This includes Custom Templates, Styles, Brushes, Images and many other very cool and useful stuff. This class is used by the runtime in order to choose and apply all these nice peaces you have put in. This brings to mind that this is one of the fundamental classes of the framework and therefore it is very well structured.

The dissapointment

This is where it gets messy. Having in mind that this is a ResourceDictionary you might think that it has all the great features that the standart Dictionary class reveals. Since it is being derived from IEnumerable interface, you might think that you can easily “foreach” that Dictionary and circle aroud your resources. Well you thought wrong ! This is how the GetEnumerator method used by the “foreach” looks like:

IEnumerable<...>.GetEnumerator()
{
throw new NotImplementedException();
}

So if you try to run this very simple code:

ControlTemplate tmp;
ResourceDictionary dict = new ResourceDictionary();
dict.Add("template1", new ControlTemplate());
dict.Add("template2", new Style());

foreach (object obj in dict)
{
if (obj is ControlTemplate)
tmp = obj as ControlTemplate;
}

you will get this nice thingy here:

NotImplementedException

The sad thing is that this was a behaviour in Silverlight 2 Final release and it is still present in Silverlight 3 Beta. I really hope this will be exposed in the Final release of Silverlight, because personally in my opinion this is quite useful in some complicated scenarios.

MergedDictionaries

This was a problem not being solved in Silverlight 2, but in the 3rd version it has been resolved, so I am not going to brag about it. Still I have been experiencing some difficulties in using them in the generic.xaml, but I am planning to write a separate article on that topic.

The conclusion

I want to say that this article of mine is not intended to mess around with the Silverlight guys from Microsoft. Silverlight is a great technology and I absolutely love it. I have been developing Silverlight applications for almost an year now and I tend to use it in every project I am involved in. I am pointing out those issues just to help the other Silverlight developers, who stumble upon those problems and can’t figure out why. I have spent days trying to fix some of the issues I am writing about and most of the time I spent on figuring out why it is happening. The more developers I can help by sharing my experience the better !

Silverlight Tip: Why do I get “An unhandled exception of type ‘System.OutOfMemoryException’ occurred in Unknown Module.” ?

In the past 4-5 days I have been busy, trying to figure out a VERY strange problem with a PropertyGrid solution we have done in Silverlight. Let me first explain quickly what is the solution about.

This is a PropertyPanel Silverlight control, used to expose the public properties of a bussiness object and allow the user to change them back to the object. Needless to say, the control had to populate these properties via reflection by itself. The tricky part is that we needed to create custom editor controls for each property type.

To make this flexible we added all the property editors as a simple ContentControl and dynamically changed the ControlTemplate of the editor, based on the type of the property.

So far so good. We have implemented the whole mechanism for populating the properties, dynamically creating the ContentControl editors and choosing the ControlTemplate from a list of templates, being predefined.

We have added all those things in a Silverlight Library solution and created another solution (a Silverlight Application, what else ?), where we made a test by passing a custom business object to the control and show it.

Everything went well when we used only the generic build-in controls from the Silverlight SDK for the editors. As soon as I have added a controls from a 3rd Party, or even from System.Windows.Controls, I got the strangest exception on loading:

“An unhandled exception of type ‘System.OutOfMemoryException’ occurred in Unknown Module.”

This was just not enough information. No stack trace, no InnerException, nothing ! I started debugging and pulling out things one by one from the functionality to get to the bottom of the problem. Nothing helped. I spent more than 4 days trying to solve it, but I couldn’t. Finally, while browsing the Google results for the message of the exception, I got to this address: http://slpropertygrid.codeplex.com/

This is a similar solution by some guys, who faced the same problem. What I have noticed is that they had the problem when the referencing DLLs were broken. As I took a look at our solution I found out that we haven’t referenced the 3rd Party DLLs in the sample solution at all ! I have only referenced them in the Library solution. One might think that this is a normal thing to do, because these DLLs should be exposed to the simple solution by the Library one. Well, one is wrong ! Turned out that Silverlight has some issues on that topic. The exception is totally misleading and if I haven’t found that solution, I wouldn’t had had solved the problem.

Blogging from MS Days 09

Here I am at Microsoft Days 2009 Conference.

It is already the end of the second day and I am waiting to hear my last session for this year’s MS Days.

My overall satisfaction with the conference is not really as I expected. The location is moved, which in my opinion, is a bad thing. So far the conference was being held in the IEC (International Exhibition Center – [Google Maps]) just at the end of Sofia. Getting there with a car is fairly easy and there is a direct bus transport, stopping at the main entrance of the Center.

This year’s conference is being held in Hotel Kempinski ([Google Maps]), which is in the town’s center. You can imagine that getting there is a nightmare, getting out of there is a nightmare and parking is a new level of nightmare. I must agree that some of the halls are bigger than the one at the IEC, but this is not really the issue the organisation guys are facing.

As it is expected I came here to hear as more Silverlight and WPF sessions as I could. So far I attended three sessions of this kind. Some were good, some were bad. Honestly I expected a storm of Silverlight 3 sessions since it is brand new and being announced about 3-4 weeks ago. Unfortunately there was only one that was regarding Silverlight 3. This is kinda dissapointing.

Another interesting session I saw was “Windows 7 – Deep Dive”. Quite an interesting one, it really got me in to wanting to install that new system. So as soon as I have the chance, I will install it and give it a go.

There was a showroom at the conference where you could check out Microsoft Surface and play around with it. Quite a cool gadget. I hope that I will have the chance to work on such a technology some day.

As a conclusion I can say that Microsoft Days 2009 was nice, but I was expecting much, much more. But this won’t stop me from attending MS Days 2010. I hope it will beat the crap out of this one !

My work computer broke

Yesterday my office computer broke down very suddenly. I just started the computer and after Windows has loaded it just crashed. After repeating this Loaded-Crashed cycle a few times, I took it to the Administrator’s office and left it there. This morning I got it back and I was amazed by the cause of the problem. Here are the photos of my video adapter. I can imagine that only a massive power shock failure might have caused this to happen.

Visual Studio on Windows Mobile

Today, while I was circling around my favourite “morning coffee” RSS feeds, I stumbled upon some of the PDC sessions on Silverlight that were held in the past few days, and I was just stunned by the very interesting session Microsoft Silverlight 2 for Mobile: Developing for Mobile Devices. I was just so fascinated by the session that I literally started laughing and everybody in the office just gazed at me in misunderstanding.

In the session you can see how Visual Studio can be run on Windows Mobile device. Yes it is pretty hard to write code and interact with the environment from the little screen of the mobile device, but still it is very very interesting !

This is not the first session from PDC, that I have watched. In the next days I will post more links and articles about the ones I have seen and what is interesting about them. Also I plan to post some articles about a few problems I stumbled upon my development in the last few month, so stay tuned !

The first Android phone is released !

Today the first phone with the Android operation system by Google, have been released. This is the HTC G1, formerly known as HTC Dream. The phone is very cool and the Android OS gives you complete integration with all Google services. Here is a presentation of what Android is capable of:

Is this the next step of Google in becoming the world’s Big Brother ?

Currently Google has all your information – contacts, calendar, e-mail, the iGoogle homepage where you put what you like. When you search Google knows what you are interested in and what you might need in order to show you intelligent advertisements. Of course Google says that all this information is strictly confidential and nobody can use it for personal gain, but is that completely true ? As we all know there is no such thing as a confidential information in the Internet world we live in. There is always a way to get it ! I rest my case…

Don’t get me wrong ! I LOVE Google and probably some time in the near future I WILL buy and Android phone. I use Google a lot, as everybody else. I have my e-mail there, contacts, calendar, iGoogle homepage and many, many more. I am just saying, be careful !

I am waiting for your comments on the subject !