Dynamically loading resource dictionary files to a

2019-01-12 00:41发布

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 -

  • I don't want to embed the xaml file as a resource
  • The current project is a .net 3.5 class library project
  • The above mergedictionary statement is written in a class belonging to the same project
  • I also added the [assembly: AssemblyAssociatedContentFile("resources/leaf_styles.xaml")] manually once I figured that this is not working (for testing)

Update

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) });

3条回答
ら.Afraid
2楼-- · 2019-01-12 01:06

At last, it worked. Here is what I did,

  1. Went thru' http://msdn.microsoft.com/en-us/library/aa970069.aspx.
  2. 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 });
    
查看更多
beautiful°
3楼-- · 2019-01-12 01:12

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);
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-12 01:19

I encountered same "missing resource problem" and scratched my head for hours. Then I realized that my assembly name contains dots (.) and changed the resource assembly name, tested again and it worked. It was a 16x16 png image file which ı wanted to load. But I see that dotted assembly names causes error for soma cases and does not cause error for other cases.

  • 1) If you are loading a style from resource, it works
  • 2) If you are loading an image, it does not work. The resource can not be found.

I used the same code for both cases but results are different. I don't know if it is a wpf bug.

查看更多
登录 后发表回答