WPF中资源的引用方法

一、引用同一个程序中的资源

1、使用相对Uri来引用资源,如下所示

img.Source=new BitmapImage(new Uri(@"d"\iamges\Background\1.jpg"));

使用相对uri: img.Source=new BitmapImage(new Uri("images/1.jpg",UriKind.Relative));

2、使用更累赘的绝对Uri:

img.Source=new BitmapImage(new Uri ("Pack://application:,,,/iamges/1.jpg"));

(这三个逗号实际上是三个转义的斜杠。换句话说,上面显示的包含应用程序URI的Pack URI 是以application:///开头)

二、引用位于其他程序集中的资源

路径的表示方法:Pack://application:,,,/AssemblyName;Component/ResourceName

例如:如果图像被嵌入到一个一引用的名称为ImageLibrary的程序集中,需要使用如下所示的URI:

img.Source=new BitmapImage(newUri(“Pack://application:,,,/ImageLibrary;Component/images/1.jpg"));

或者从更实用的角度,可以使用等价的相对Uri

img.Source =new BitmapImage(new Uri("ImageLibrary;Component/images/1.jpg",UriKind.Relative));

三、使用到了版本号的和公钥标识的强命名程序集

img.Source=new BitmapImage(new Uri("ImageLibrary;v1.25;sd2sw34e432f;Component/Images/1.jpg",UriKind.Relative));

原文地址:https://www.cnblogs.com/zerommc/p/11598336.html