Posts
58
Comments
405
Trackbacks
0
Refer to resources in WPF with Pack URI

It's been over 2 months since I last updated my blog.  I have been busy; we got a new VP of IT and he made some changes and there are a bunch of stuff that's getting rejigged and people moved, projects retargeted, etc. etc.  In the end, it's not enough reason for me to not blog at all.

I started this blog so I can record my experiences with learning new stuff.  I have to say that I came back to this blog more than enough times to relook at the stuff I had learnt, to the point where my lapse in adding new stuff actually prevents me from being very productive - I learned some stuff that I should've put in here but I didn't, and in the end have to relearn it again since I didn't record it. D'uh.

Anyway, I need to be more disciplined and will try to be more active.  Relearnt something today about how to access file resources in WPF, instead of using the typical StaticResource or DynamicResource - use Pack URIs, as further detailed here.  The 2 important ones for me to remember are referencing items in the same assembly and in a different assembly (use the following format with the Source attribute in xaml):

pack://application:,,,/ResourceFile.xaml

pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

This is actually very relevant to me; sometime last week, we had decided on trying to consolidate all our resources into a single DLL for each team project we have - it helps with internationalization (minimizing the number of DLLs), easier checking for entries that may already exist, can be used by other applications / modules if necessary, and can be swapped with another resource DLL or a newer one.

With our application, we won't actually use the pack URI to point to different XAML (we may, but I see the use to be fairly limited in that regard), but we will use the pack URI to point to proper image / icon / bitmap files - so it's nice if we can consolidate all those files into a single DLL.

posted on Monday, April 28, 2008 1:41 PM Print
Comments
Gravatar
# re: Refer to resources in WPF with Pack URI
Craig
2/13/2011 4:41 AM
Hello,
After spending many hours searching I found a very simple method, I found no example and so I share mine here
which works with images. (mine was a .gif)

Summary:

It returns a BitmapFrame which ImageSource "destinations" seem to like.

Use:

doGetImageSourceFromResource ("[YourAssemblyNameHere]", "[YourResourceNameHere]");

Method:

static internal ImageSource doGetImageSourceFromResource(string psAssemblyName, string psResourceName){
Uri oUri = new Uri("pack://application:,,,/" +psAssemblyName +";component/" +psResourceName, UriKind.RelativeOrAbsolute);
return BitmapFrame.Create(oUri);
}

Learnings:

From my experiences the pack string is not the issue, check your streams and especially if reading it the first time has set the pointer
to the end of the file and you need to re-set it to zero before reading again.

I hope this saves you the many hours I wish this piece had for me!

Thanks,
Craig
Platinum Salon Software
http://www.SalonSoftwareSystem.com

Post Comment

Title *
Name *
Email
Comment *