Consuming resources from external assemblies in Silverlight 4

September 24, 2010

There’s a bug in Silverlight 4 that prevents you from being able to use resources that are declared in a ResourceDictionary in a referenced assembly.  The error is a XamlParseException whose message is “Failed to assign to property ‘System.Windows.ResourceDictionary.Source’.”.  The problem is that if you have a Silverlight class library whose name ends with “.Resources” it blows up at runtime.  To fix the problem, you must change the assembly name (in the project properties) so that it does not end with “.Resources” but, perhaps, ends with “.Resources.YouHaveToBeKiddingMe“. 😉

Here is an example of using a resource dictionary in an external assembly that will blow up at runtime:

Here is a fixed version:

If you compile a Silverlight application against a project whose assembly’s name ends with “.Resources” that DLL won’t even get included in the XAP or listed in the AppManifest.xml file.  It simply gets ignored, which lead to hours of head-bashing debugging for me.  I hope this workaround saves others the grief!


The ultimate hack for Silverlight in Blend

January 6, 2010

I recently needed to write some design-time support for a Silverlight framework.  I needed to discover a solution’s assemblies while running in Blend, and then pass those assemblies downstream for further processing.  In WPF, using the full .NET framework, that’s easy: just call AppDomain.CurrrentDomain.GetAssemblies() and then filter them based on the Location property.  In Silverlight, however, a couple of problems showed up that seemed to make this task impossible.

First, the Assembly.Location property is marked ‘Security Critical’ in Silverlight 3.  This means that if you access the property, an exception will be thrown.

Second, Silverlight 3’s AppDomain class does not have a GetAssemblies method!

It turns out that both of these problems are easy to overcome.  The key thing to know is that when Silverlight code is running in Blend, it’s actually executing against the regular .NET Framework (the same one that WPF uses).  You can access ‘Security Critical’ members and no errors occur.  That takes care of the Assembly.Location issue.  Even though the Silverlight code you write, that is intended to run in Blend, can only access the Silverlight API, you can still access the full .NET framework’s API via reflection.  This allows you to work around the second issue.  For example:

var assemblies = typeof(AppDomain)
   .Invoke(AppDomain.CurrentDomain, null);

Remember, this hack only works when your Silverlight code is running in Blend! This will not work when running under normal circumstances, since Silverlight apps normally only have access to the Silverlight subset of the .NET Framework.

Happy coding!

Routed Events in Silverlight 3

May 15, 2009

I started working at IdentityMine a few weeks ago, and have been exposed to several very interesting projects made by some very smart people.  One project that I think has a lot of potential was created by Andrew Whiddett.  He created a library that adds in-depth routed event support in Silverlight 3.  If you are working with Silverlight, I highly suggest you check this exciting project out!  I’m very interested in seeing where this goes…

If you have any feedback on that library, please leave it on the project’s Discussions board.  Enjoy!

Slick Silverlight Content Navigation Design

March 10, 2009

In the recent past, I worked quite a  bit on a new Samples Browser application for the Infragistics Silverlight Data Visualization product.  This post is not intended to be a plug for the product, but I want to point out the application’s UI navigation design.  I can’t take credit for dreaming this one up (props to Grant for that), but I implemented most of it and added some goodness to the design along the way. Naturally, I didn’t do any of the visual design work — which should be obvious, considering how good this thing looks! 😀

When you first load the application up, it looks like this (click on the following images to see them at full size):


This screen allows you to either check out some “showcase samples” or to view demos for each of the four controls in the product, which are represented by “boxes” toward the bottom.  Also notice the start of a breadcrumb trail toward the top, which currently only has an item in it for “Home.”  Now suppose you were to click on the XamWebMap box down below.  An animated transition would bring you to this screen…


Each item in the list represents a sample for the XamWebMap control, and even has a thumbnail image next to it.  Since there are currently ten samples available for XamWebMap, and this view only shows eight items at a time, you can click the arrow button on the right side to slide the other samples into view…


Notice that the breadcrumb trail above now contains the selected control as the next item in the list, in this case “XamWebMap.”  If we were to select one of the map samples, the breadcrumb trail would then contain the selected sample in view, like this…

united states sample

Now here’s where it gets really cool.  The breadcrumb trail also provides dropdown lists to make it easier to jump to the exact content you are interested in, if sliding from sample to sample isn’t what you want to do.  Here’s what happens when you click on the breadcrumb item for the selected sample…

select a sample

You could also click on the middle breadcrumb item’s dropdown indicator to get a list of the available controls, and then see a tiled view of that control’s samples.  Pretty slick, eh?!  As you can probably tell, I’m really proud of how this app turned out.  8)

One more point of interest is that you can view the XAML and C# code for each sample.  When you’re viewing a sample, click on the “CODE” icon in the top right corner of the sample and the sample will perform a 3D flip to reveal the XAML/Code view…

XAML/Code View

While this wasn’t a trivial user interface to implement with Silverlight 2, it wasn’t that hard.  I think it provides an excellent user experience, especially because of the smooth transitions from content to content, and the ability to either browse or navigate directly to something.

If you want to check this application out, here’s the link:

My Mix 10K Submission

January 16, 2009

Microsoft is hosting a programming contest called Mix 10K.  The idea is that you can submit an application whose binary file size is no greater than ten kilobytes, and then you stand the chance to win some cool prizes.  I don’t expect to win anything, but I still had fun creating my submission: i LOVE your cursor

I know, it’s a stupid program, but I get a kick out of it.  😀

Emulating ICommandSource in Silverlight 2

November 17, 2008

I recently discovered that Silverlight 2 has the ICommand interface, but does not have ICommandSource.  Basically, this means that you cannot easily execute a command when, say, a user clicks a button.  This is pretty strange to me, and renders Silverlight’s ICommand interface next to worthless.  If you’re an MVVM zealot like me, I’m sure you must see where I’m coming from here…

So, I corrected the situation.  You can now execute commands, pass them parameters, and even honor their CanExecute status, all via some attached property magic.  Read more about it, and download the source code, here.

My first experience with Silverlight 2.0

December 13, 2007

I have spent the past couple of days checking out Silverlight 2.0.  I have much to say about my findings thus far, but in general, I am both shocked and pleasantly surprised.  Since Silverlight 2.0 is an alpha, there is no point in being too critical of it just yet.  According to Scott Guthrie the next RTM release, called Silverlight 2.0, will be much more feature-rich than the current alpha.

I expected Silverlight 2.0 to be an enormous, complex, sophisticated platform rivaling the technical splendor known as WPF.  I mean, it used to be called WPF/E , right? Boy was I wrong!  I just kept saying to myself, “I can’t believe Silverlight does not have that either!”  At first, it felt like I was trying to play a piano that had all of its white keys removed, and I could only use the black keys to make music.

Here is a brief list of some things in WPF that we know and love but are currently missing from SL 2.0:

  • Standard controls; like Button, TextBox, ListBox, CheckBox, etc.
  • Layout panels
  • Data binding
  • Templates
  • Styles
  • Commands
  • Routed events
  • A vast number of events on elements seen in WPF
  • A resource system based on merged resource dictionaries
  • Visual and logical trees (no programmatic construct represents them, at least)
  • Did I mention that there’s no Button???

Once I got past all those missing necessities, I found that what does currently exist is actually very cool.  If Microsoft delivers all that Guthrie promised in his blog post (which does not include everything in the list I provided above), SL 2.0 should be an excellent platform. 

I must admit, though, I really miss routed events.  Now that I think about events as tunneling and bubbling, working with normal CLR events really stinks.  It feels so primitive.  However, I might have found something that indicates Microsoft intends on adding routed events into SL 2.0 (even though Guthrie did not mention this in his post).  The EventTrigger class exposes a RoutedEvent property, just like in WPF.  It is possible that they named SL’s EventTrigger’s property “RoutedEvent” to ensure that the Silverlight XAML compiles in WPF, but perhaps they actually intend on implementing routed events in the future.  Think about it, what sense would it make to have a property called RoutedEvent if routed events do not exist in SL?

I tried watching some of the video tutorials on that go over the basics, but most of them were so dumbed-down that I felt like I was in a Special Ed class.  I have found the Quick Start tutorials to be very helpful and insightful for a newbie, such as myself.

For my first SL 2.0 project I decided to build a very simple ListBox control.  Please keep in mind that this is the first Silverlight project I have ever written, so don’t expect anything too cool.  After creating this ListBox I found out that the Silverlight SDK comes with the source code for a ListBox control.  I checked out the SDK control, and it is definitely better than mine is, but at least mine allows you to navigate the items with your keyboard.  Ahha!  Take that, Microsoft!!  😉

Below is a screenshot of my AgListBox control in action, with some text above it explaining which item is selected (in case you couldn’t already tell…):

AgListBox in action

Here is My First Silverlight Project.   Be sure to change the file extension from .DOC to .ZIP and then decompress the file.  If you need to install the Silverlight runtime and tools, check out the list of requirements and links here.

2D physics in Silverlight 1.1

May 23, 2007

Chris Cavanagh, the man who came in second place in The WPF Challenge for his WPF Physics demo, is at it again.  This time he has built a Silverlight 1.1 app on top of a physics engine.  I’m amazed that Silverlight can handle this type of scenario!  As Chris points out in his blog post about the demo app, there seems to be some performance issues in Silverlight which cause the animations to “freeze” for a short time.  Other than that, the demo is very cool!  Check it out here: