I am trying to add a xaml resource file dynamically using the statement,
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("resources/leaf_styles.xaml", UriKind.Relative) });
This is throwing an exception, Cannot locate resource 'resources/leaf_styles.xaml'.
I added the leaf_styles.xaml file to the project under resource folder and the BuildAction is set to "Content", CopyAlways is set to True. Still I get this error. Could some one help me out pointing whats wrong??
Additional information -
If I give it as an absolute location, it is working properly.
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"D:\foo\trunk\bin\resources\leaf_styles.xaml", UriKind.Absolute) });
At last, it worked. Here is what I did,
Changed the Uri pattern to
var foo = new Uri("pack://siteoforigin:,,,/resources/leaf_styles.xaml", UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });
To load a content file, you can call the GetContentStream method of the Application class, passing a pack URI that identifies the desired content file.
Checkout
http://msdn.microsoft.com/en-us/library/aa970494.aspx#Content_Files
EDIT
I did it successfully like this
Uri uri = new Uri("Resources/MyDict.xaml", UriKind.Relative);
StreamResourceInfo info = Application.GetContentStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
ResourceDictionary myResourceDictionary =
(ResourceDictionary)reader.LoadAsync(info.Stream);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With