在WPF中使用URIs (uniform resource identifiers)标记和加载文件,有几种方式:
- 在程序运行时,指定用户界面(UI);
- 加载图像;
- 页面间导航;
- 加载非执行的二进制文件。
而且,使用URIs可以从很多位置标记和加载文件:
- 当前的程序集;
- 引用程序集;
- 一个程序集的相对位置;
- 应用程序的原始网页地址。
Pack URI机制
与常用的http,ftp,file等众所周知URI前缀,pack URI使用 pack
pack://authority/path
authority指定包含part的Package的类型。
path指定在Package中Part的位置。
Package与Parts的关系与application和files相似。一个application(package)可以包含多个files(parts):这些文件包括:
- 编译在本程序集中的资源文件;
- 编译在被引用程序集中的资源文件;
- 编译在引用程序集中的资源文件;
- 内容文件;
- 原始站点中的文件。
WPF支持两种authority , 分别是application:///和siteoforigin:///
application:/// 指定的应用程序数据文件,在编辑的时候是已知的,包括资源和内容文件。
siteoforigin:/// 指定原始文件的站点(The siteoforigin:/// authority identifies site of origin files.)
备注:The authority component of a pack URI is an embedded URI that points to a package and must conform to RFC 2396. Additionally, the "/" character must be replaced with the "," character, and reserved characters such as "%" and "?" must be escaped. See the OPC for details.
资源文件的Pack URIs
本地程序集中的资源文件
Authority: application:///
Path: 资源文件的名字,包括路径,相对于本地程序集的项目根目录。
例如: pack://application:,,,/ResourceFile.xml
pack://application:,,,/subfolder/ResourceFile.xaml
引用程序集中的资源文件
Authority: application:///.
Path: 被引用资源的文件名称. Path的格式如下:
AssemblyShortName{;Version]{;PublicKey];component/Path
-
AssemblyShortName: the short name for the referenced assembly.
-
;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.
-
;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.
-
;component: specifies that the assembly being referred to is referenced from the local assembly.
-
/Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder.
例如:
pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml
pack://siteoforigin:,,,/SomeAssembly;com
ponent/ResourceFile.xaml
内容文件的Pack URIs
Authority: application:///.
Path: The name of the content file, including its path relative to the file system location of the application's main executable assembly.
例如:
pack://application:,,,/ContentFile.xaml
pack://application:,,,/Subfolder/ContentFile.xaml
备注:html内容文件不能被定位。需要使用 site of origin
Site of Origin Pack URIs
Authority: siteoforigin:///.
Path: The name of the site of origin file, including its path relative to the location from which the executable assembly was launched.
例如:
pack://siteoforigin:,,,/SiteOfOriginFile.xaml
pack://siteoforigin:,,,/Subfolder/SiteOfOriginFile.xaml
Page Files
被设置为MSBuild page的xaml文件被编译到程序集中,和资源文件的访问方式一样。
被编译为page的xaml文件一般用这些对象作为根元素:
绝对和相对Pack URIs
绝对Pack URIs包含完整的Scheme,authority和path
为了简化可以使用相对Pack URIs
例如:
绝对Pack URIs: pack://application:,,,/ResourceFile.xaml
相对Pack URIs: /ResourceFile.xaml
备注:site of origin只能使用绝对Pack URIs.
默认相对pack URIs是相对与引用资源的markup和代码的位置。如果前面加上\则是相对于应用的根目录。
Pack URI Resolution
在编程时使用Pack URIs
在WPF中许多地方使用pack URIs包括: