Josh Smith

I've spent far too long trying to get this to work. Here's my situation:

I need to merge resource dictionaries into my Application's Resources. The resource dictionaries exist in arbitrary assemblies somewhere on the local file system. I subclassed ResourceDictionary to create "SkinDictionary" and in its ctor I set base.Source to a Uri. The Uri uses the pack syntax to reference the assembly and its resource dictionary.

If I put the "resource assemblies" in the same folder as the EXE, this works just fine. However, our reqs don't allow for the resource assemblies to be in that folder. They might be in a subfolder of that directory, or perhaps in some totally different place. If I put the assemblies anywhere else other than the startup directory, the pack system won't work. I assume that I'm messing up somewhere...

Here's the Uri I use to load the resource assembly when it's in the startup dir:

string pack = String.Format( @"pack://application:,,,/{0};component/{1}", _resourceAssemblyPath, fileName );

// _resourceAssemblyPath == "SomeAssembly" fileName == "SomeFile.xaml"

base.Source = new Uri( pack, UriKind.Relative );

That works. (Hallelujah!)

My question is, what needs to be changed so that I can load the resource assemblies from some other directory I've tried all types of things, read the docs, looked over blog entries, etc. It's just not happening. I keep getting exceptions saying things like "Cannot locate resource xyz".

Thanks for any help!




Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Zhou Yong

Hi Josh, I've sorta answered this question in this thread:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=934538&SiteID=1

Tell me if using configuration file can help you a bit.

Sheva





Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Josh Smith

Zhou,

Thanks for the link. Unfortunately that technique won't work in my situation. My EXE is installed under the Program Files dir, but the resource assemblies are in the user's Application Data dir. The resource assemblies cannot be in a sub-folder of the startup dir (I found that out after writing the initial post in this thread). I suppose I'll have to use reflection to load the assemblies and then extract the resources manually. Sad

Is there no way to use the pack URI syntax to reference an assembly at an arbitrary location on the local file system If not, the pack notation has a serious deficiency!

Thanks,

Josh






Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Zhou Yong

Hi Sorry, I misunderstood you a bit peviously, you wanna put the resource assemblies in an arbitrary location.
I think this is not a problem with pack URI, it is actually a limitation imposed by the CLR loader. when loading assemblies into appdomain, CLR loader will look in several places for the interested assemblies, those places includes the global assembly cache, the startup directory which your executable resides or the sub-directories under this start up directories, and the temporary shadow copy directories(shadow copy directories are heavliy used by ASP.NET for dynamically compiled assemblies), or optionally the codebase which probably will point to a remote location. So basically the CLR loader will only look for assemblies in those places. So Assembly.LoadFrom is probably an option to consider.

Sheva





Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Josh Smith

Thanks again, Zhou. Perhaps I misunderstand the intended capabilities of the pack URI scheme. I am under the impression that I could, somehow, use a file path (eg. ..\..\boo\foo.xaml) and that could be resolved to a location on the local file system. If that is the case then the CLR's probing behavior would not come into play. Am I wrong Does the pack scheme not support pointing to arbitrary directories Does it fallback onto the CLR's probing behavior I haven't seen this documented, but I might have just missed it.

Thanks for any more help! Smile






Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Josh Smith

Zhou,

With your help I found the answer! Big Smile Thanks!

The trick is to load the assembly via Assembly.LoadFrom(...) and then use Application.LoadComponent(...). After the resource assembly has been loaded into the app's AppDomain the pack URI passed to LoadComponent will work. Ex:

Code Snippet

Assembly.LoadFrom(resourceAssemblyFilePath);

string packUri = String.Format(@"/{0};component/{1}", assemblyName, RESOURCE_FILE_NAME);

Uri uri = new Uri(packUri, UriKind.Relative);

return Application.LoadComponent(uri) as ResourceDictionary;

I'm not sure if this would work in a situation where I load the resource assembly into a child AppDomain, but I can live with that...for now. Wink

Thanks!






Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Shannon Braun - Sysknowlogy

Josh,

Another option is to tell the clr where to find the resource assembly using the application configuration file. The first <assemblybinding/> below shows how to do it for sub directories in your application private directory and the second <assemblybinding/> shows how to do it for an assembly that is found in another directory... which I believe is what you were trying to accomplish.

< xml version="1.0" encoding="utf-8" >

<configuration>

<runtime>

<assemblyBinding xmlns="urnTongue Tiedchemas-microsoft-com:asm.v1">

<!--use this for subdirectories with in your application,

you can add more then one by using ; to seperate them-->

<probing privatePath="skins"/>

</assemblyBinding>

<!--use this for subdirectories with in your application,

you can add more then one by using ; to seperate them-->

<assemblyBinding xmlns="urnTongue Tiedchemas microsoft com:asm.v1">

<dependentAssembly>

<codeBase version="1.0.0.0" href= "file:///c:\WPFSkins\skin1.dll"/>

</dependentAssembly>

</assemblyBinding>

</runtime>

</configuration>

Shannon





Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Josh Smith

Thanks Shannon. I had read this post by Pavan Podila which discusses that technique: http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!193.entry My technical requirements don't allow for that option, but it is definitely a good way to go if you can. Thanks for the feedback! Smile




Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Shannon Braun - Sysknowlogy

Sorry... must of missed that in the thread.



Re: Windows Presentation Foundation (WPF) How to load resource dictionary from external assembly via pack?

Josh Smith

No worries. Smile It's great that you added a XML config sample to this thread, for anyone who finds this via a search engine. There will be demos of multiple solutions.