I Am Not Myself

Bills.Pay(Developer.Skills).ShouldBeTrue()

Loading an Image from Resources in an Unreferenced Assembly

I have two silverlight assemblies, CaseManager.Applet and CaseManager.Applet.Sample. The Sample assembly has a reference to the base Applet assembly. The sample assembly has an embedded resource png image. The base applet assembly has a view xaml that I wish to display that image programmatically.

In the sample assembly I have a bit of code that creates a Uri like so:

private void RegisterIcon()
{
 

  var icon = new AppletIcon()
               {
                 ImageUri = new Uri("CaseManager.Applet.Sample;component/images/guiness_2.png", UriKind.Relative),
                 ModuleType = GetType(),
                 Text = "Sample Module"
               };

  container.RegisterInstance("SampleModule", icon);
}

When I execute this code all the properties of ImageUri throw InvalidOperationException. I am not sure why. I have a hunch that it has to do with the way the assemblies are referencing each other in one direction. Anyone have suggestions?

Update: ok I found it by using the googles searching for WPF instead of silverlight info. The following code does the job:

ImageUri = new Uri("/CaseManager.Applet.Sample;component/images/guiness_2.png", UriKind.Relative),

Things to note here:

  • The slash at the start of the Uri string.
  • The short name of the assembly containing the resource.
  • the ;component/ section.

From there it is basically the path inside your project to the image. Hope this helps someone else.

For what it was worth I was missing the very first slash. /facepalm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: