这一篇单独拿出来分析这个程序集资源,为的就是不想让大家把程序集资源和exe程序强关联,因为程序集资源实际上是二进制资源,后续编译过程中会被嵌入到程序集中,而为了更方便的使用资源,我们要好好梳理一下程序集资源相关的知识。(例如多语言资源,多工程、多项目使用的公共资源文件)。

1)在程序集中添加资源

我们通过向项目添加文件并尝试修改资源文件属性看有什么不同的结果。

在工程上右键=》添加=》新建文件夹=》改名为images=》回车=》在images文件夹上右键=》添加=》现有项=》右下角文件类别选择所有文件=》找到你想导入的图片=》点击添加,结果如下图.

这样就添加好了一个文件,我们在添加的图片上右键属性看到生成操作为resource,我们添加代码如下:

<window x:class="applicationresources.mainwindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:applicationresources"
  mc:ignorable="d"
  title="mainwindow" height="450" width="800">
 <grid>
  <image source="/images/takenotes.png" width="230" height="130"/> 
 </grid>
</window>

我们在工程上点击编译,生成成功后我们去看编译结果,发现文件夹下没有这个image,我们使用反编译ilspy工具查看生成的这个exe,我们看到png文件作为资源被放在了resources里,(这里有一个小插曲,不知道为什么,我的png图片在最开始反编译的时候没有。直到我添加了上面image的代码反编译才出来。以后会分析这个问题。这里记录一下)

而修改图片的属性为content并修改复制到输出目录为始终复制。重新编译工程。

我们看到了debug文件夹下多了一个images文件夹,里面包含了我们的图片。而反编译程序集集中就没有这个资源文件了,它从资源里更换到目录下了。这样的话,程序集因为没有包含了资源文件大小就发生了变化。

通过修改资源的属性,资源在编译过程中会放置在不同的地方,这是其中2种比较常用的设置属性。通过这种设置程序集资源的方式易于更新,只需要在桌面资源管理器种替换掉文件并重新编译程序就能完成资源文件的替换,非常方便。资源文件可以放在主工程文件下,也可以选择放在单独的dll下,具体看你的设计需要啦。因为接下来我们会详细讲如何读取这些资源。即使跨了dll。

2)在程序集中查找资源

  因为是在讲application的资源所以我们先看application下对资源的查找方法,在app.xaml的application上按下f12:

我们看到了返回streamresourceinfo类型的方法又3个。getcontentstream()、getremotestream()、getresourcestream()。这里只讲getresourcestream()其他的可以自己在对应的方法上按f1查看文档。(记得把资源属性设置成resouce)

我们使用这种在程序集管理所有资源的方式,我们可以自己实现很多种自己管理资源的想法。可以把代码添加到自己的工程里调试一下看看自己感兴趣的内容,代码如下:

using system;
using system.collections;
using system.diagnostics;
using system.globalization;
using system.io;
using system.reflection;
using system.resources;
using system.windows;
using system.windows.resources;

namespace applicationresources
{
 /// <summary>
 /// mainwindow.xaml 的交互逻辑
 /// </summary>
 public partial class mainwindow : window
 {
  public mainwindow()
  {
   initializecomponent();
   //方法1使用streamresourceinfo接收getresourcestream()读取到的内容。
   streamresourceinfo sri = application.getresourcestream(new uri("images/takenotes.png", urikind.relative));
   string type = sri.contenttype;
   var stream = sri.stream;
   //方法2,还记得我们反编译时候看到的资源吗?从assembly取我们要的资源。
   assembly assembly = assembly.getassembly(this.gettype());
   string resourcename = assembly.getname().name + ".g";
   //单个资源
   resourcemanager rm = new resourcemanager(resourcename, assembly); 
    using (resourceset set = rm.getresourceset(cultureinfo.currentculture, true, true))
    {
     unmanagedmemorystream s = (unmanagedmemorystream)set.getobject("images/takenotes.png", true);
    }
   //遍历所有资源
   resourcemanager rmresources = new resourcemanager(resourcename, assembly);
   using (resourceset set = rmresources.getresourceset(cultureinfo.currentculture, true, true))
   {
    foreach (dictionaryentry item in set)
    {
     debug.writeline($"resourceset dictionaryentry as {item.key.tostring()}");
    }
   }

  }
 }
}

wpf目前给我们封装了一些访问资源的方法,我们使用xaml和cs下传入相对和绝对路径来演示:

<window x:class="applicationresources.mainwindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:applicationresources"
  mc:ignorable="d" loaded="window_loaded"
  title="mainwindow" height="450" width="800">
 <grid>
  <stackpanel> 
  <image source="images/takenotes.png" width="220" verticalalignment="top" horizontalalignment="left"/>
  <image source="d:\image\takenotes.png" width="220" verticalalignment="top" horizontalalignment="left"/>
   <image x:name="imgabsolutepath" width="220" verticalalignment="top" horizontalalignment="left"/>
   <image x:name="imgrelativepath" width="220" verticalalignment="top" horizontalalignment="left"/>
  </stackpanel>
 </grid>
</window>
 private void window_loaded(object sender, routedeventargs e)
  {
   imgabsolutepath.source = new bitmapimage(new uri(@"d:\image\takenotes.png", urikind.absolute));
   imgrelativepath.source = new bitmapimage(new uri("images/takenotes.png",urikind.relative)); 
  }

在这个基础上wpf支持pack uri。pack uri语法可以寻址包含在编译过的程序中的资源,(从名字上理解package uri?)使用pack uri可以让我们更加规范的使用资源文件,也更加方便。

如下这2段代码是等效的因为没有跨程序集。

 <image source="images/takenotes.png" width="220" verticalalignment="top" horizontalalignment="left"/>
 <image source="pack://application:,,,/images/takenotes.png" width="220" verticalalignment="top" horizontalalignment="left"/>

我新建了一个程序集,放置在imagelibrary程序集下同样目录的资源。

xaml写法:

   <image source="images/takenotes.png" width="120" verticalalignment="top" horizontalalignment="left"/>
   <image source="pack://application:,,,/images/takenotes.png" width="120" verticalalignment="top" horizontalalignment="left"/> 
   <image source="pack://application:,,,/imagelibrary;component/images/takenotes.png" width="120" verticalalignment="top" horizontalalignment="left"/> 

cs写法:

private void window_loaded(object sender, routedeventargs e)
  {
   //绝对路径
   imgabsolutepath.source = new bitmapimage(new uri(@"d:\image\takenotes.png", urikind.absolute));
   //相对路径
   imgrelativepath.source = new bitmapimage(new uri("images/takenotes.png",urikind.relative));
   //当前程序集pack uri 语法
   imgrelativepath.source = new bitmapimage(new uri("pack://application:,,,/images/takenotes.png"));
   //跨程序集使用资源 pack uri 语法
   imgcrossassemblypath.source = new bitmapimage(new uri("pack://application:,,,/imagelibrary;component/images/takenotes.png")); 
  }

这样就可以把资源单独存放在一个dll里。然后剩下的就看自己怎么使用拉。

我们主工程下一般会保持当前app.xaml的当前资源结构。我们把资源都放在 application.current.resources.mergeddictionaries内。通过mergeddictionaries更好的动态替换同类型的资源(比如多语言的一种实现方式)。

var targetresource = application.current.resources.mergeddictionaries.firstordefault(x => x.source != null && x.source.originalstring.contains(uri));
 if (targetresource != null)
 {
   application.current.resources.mergeddictionaries.remove(targetresource);
 }
 //添加与系统语言对应的语言包
 application.current.resources.mergeddictionaries.add(new resourcedictionary
 {
  source = new uri($"pack://application:,,,/applicationresources;component/lang/{rcname}.xaml", urikind.relativeorabsolute)
 });

这样的话,就能辅助我们更好的使用资源拉,是不是发现写代码如果保持正确的解耦,写代码会就越来越简单?

以上就是c# wpf如何更好的使用application程序集资源的详细内容,更多关于wpf使用application程序集资源的资料请关注www.887551.com其它相关文章!