Pages

Tuesday, April 13, 2010

Pack URI to Reference Component

WPF comes with a new URI Scheme which one can use to reference a file that is packaged as resource into the assembly. Rather than using DynamicResource, or specifying the absolute path for the Resource (which would eventually translate itself to relative reference always), the actual path can also be restored using pack:\\ URI Schema.

Usage 

Pack Uri entails the Open Packaging Convension which determines organization and identification of Resources. Let us look into this in detail using few examples :

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

Here the application will find the file named ResourceFile.xaml directly to the Application Directory.  Thus to refer to any file located in current assembly and which is in Root folder, you might use this. 

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

If the file is located within a subfolder, to refer to it, you need to use Relative path from the root directory. Here you can see there is a subfolder named Subfolder where the ResourceFile.xaml is stored.

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

If you want to refer to the resource using the Referenced dll, you might use this. Remember the ReferencedAssembly refers to the name of the Assembly. Also remember if you place the ResourceFile.xaml in your root directory of referencedAssembly you should specify the path using component/ResourceFile.xaml. Therefore you can see, every file that is referenced through the ReferencedAssembly should always be relative to component folder.  So for instance
pack://application:,,,/MyCustomdll;component/MyResource.xaml. 


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

Similar to one specified above, if the resource is placed within a subfolder within the assembly, you need to again reference that relative to component folder.

5. pack://siteoforigin:,,,/Subfolder/ResourceFile.xaml

Finally if say your file is located to an assembly to which the current assembly is referenced to, you might use siteoforigin in this case to refer to a file located to the parent of this assembly.

Here you must remember that using this type of urls will make the dll tightly coupled with one another. Thus it will expect the dll which reference it to contain a file inside subfolder of its Resource tree structure.

Conclusion

It is really new to everybody to use these type of urls in WPF applications. Even I am also surprised to see such a new addition. But now I an using it, and also it works great to me. Try out them yourself, I hope you will find them interesting .

Thanks for reading.
Reference
For further reference you can read this Article

No comments:

Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.