giacoder
giacoder

Reputation: 980

ResourceDictionary replacement from code

I need to download RecourceDictionary from net and connect it to the project.

So in App.xaml I have something like a stub with current ResourceDictionary:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Then I am downloading and storing new ResourceDictionary (ResourceDictionary2.xaml) to IsolatedStorage. Then I need to replace the current one with the new one.

In fact IF this another file WOULD BE right in a project folder I would make like this:

var newDict = new ResourceDictionary { 
     Source = new Uri("/WP7ResourceDictionaryTest;component/ResourceDictionary2.xaml", UriKind.Relative) 
};
ResourceDictionary appResources = App.Current.Resources;
appResources.MergedDictionaries.RemoveAt(0);
appResources.MergedDictionaries.Add(newDict);

this code works.

So the question is: how to replace current RecourceDictionary with the file which is in IsoStorage? I tryed to set Uri like this:

newDict.Source = new Uri("isostore:/ResourceDictionary2.xaml");

but it doesn't work.

Upvotes: 0

Views: 533

Answers (1)

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

First of all you should use my Generic Isolated Storage Utility found here

Then do the following:

ResourceDictionary isoResourceDictionary = (ResourceDictionary)IsolatedStorage_Utility.Load<ResourceDictionary>(filename);
appResources.MergedDictionaries.RemoveAt(0);
appResources.MergedDictionaries.Add(isoResourceDictionary);

The next problem is you HAVE to do this before InitializComponents, because Silverligh/WP7 ONLY allows for StaticResources. The only way around this is to refresh your current page.

Upvotes: 1

Related Questions