动态加载资源字典文件到wpf应用程序会出现错误

发布于 2024-11-07 13:59:15 字数 852 浏览 0 评论 0原文

我正在尝试使用以下语句动态添加 xaml 资源文件,

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("resources/leaf_styles.xaml", UriKind.Relative) });

这会引发异常,无法找到资源“resources/leaf_styles.xaml”。

我将 leaf_styles.xaml 文件添加到资源下的项目中文件夹,BuildAction 设置为“Content”,CopyAlways 设置为 True。我仍然收到这个错误。有人可以帮我指出哪里出了问题吗?

附加信息 -

  • 我不想将xaml文件作为资源嵌入
  • 当前项目是.net 3.5类库项目
  • 上面的mergedictionary语句是这样写的在属于同一项目的类中
  • 一旦我发现这不起作用(对于测试)

更新

如果我将其指定为绝对位置,则它可以正常工作。

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"D:\foo\trunk\bin\resources\leaf_styles.xaml", UriKind.Absolute) });

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下客 2024-11-14 13:59:15

最后,它成功了。这就是我所做的,

  1. 经过' http://msdn.microsoft.com/en-us/library /aa970069.aspx
  2. 将 Uri 模式更改为

    var foo = new Uri("pack://siteoforigin:,,,/resources/leaf_styles.xaml", UriKind.RelativeOrAbsolute);
    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });
    

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 });
    
何其悲哀 2024-11-14 13:59:15

要加载内容文件,您可以调用 GetContentStream 方法应用程序类,传递标识所需内容文件的包 URI。

查看

http://msdn.microsoft.com/en-us/library/aa970494.aspx#Content_Files

编辑

我这样做成功了

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

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);
土豪我们做朋友吧 2024-11-14 13:59:15

我遇到了同样的“缺少资源问题”并挠了我的头几个小时。然后我意识到我的程序集名称包含点(.)并更改了资源程序集名称,再次测试并且它有效。这是我想要加载的 16x16 png 图像文件。但我发现点式程序集名称会导致 soma 情况出现错误,而不会导致其他情况出现错误。

  • 1) 如果您从资源加载样式,则它可以工作
  • 2) 如果您正在加载图像,则它不起作用。找不到资源。

我对这两种情况使用了相同的代码,但结果不同。不知道是不是wpf的bug。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文