Uri(uniform resource identifiers)的用法

Uri(uniform resource identifiers)

通过它可以在程序的执行过程中,加载file, image等。

Uri有两种类型,绝对路径和相对路径,下面是两种类型的写法:

注意component是关键字。

Table 3: Absolute Pack URIs in Code

File

Absolute pack URI

Resource file - local assembly

Uri uri = new Uri("pack://application:,,,/ResourceFile.xaml", UriKind.Absolute);

Resource file in subfolder - local assembly

Uri uri = new Uri("pack://application:,,,/Subfolder/ResourceFile.xaml", UriKind.Absolute);

Resource file - referenced assembly

Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml", UriKind.Absolute);

Resource file in subfolder of referenced assembly

Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml", UriKind.Absolute);

Resource file in versioned referenced assembly

Uri uri = new Uri("pack://application:,,,/ReferencedAssembly;v1.0.0.0;component/ResourceFile.xaml", UriKind.Absolute);

Content file

Uri uri = new Uri("pack://application:,,,/ContentFile.xaml", UriKind.Absolute);

Content file in subfolder

Uri uri = new Uri("pack://application:,,,/Subfolder/ContentFile.xaml", UriKind.Absolute);

Site of origin file

Uri uri = new Uri("pack://siteoforigin:,,,/SOOFile.xaml", UriKind.Absolute);

Site of origin file in subfolder

Uri uri = new Uri("pack://siteoforigin:,,,/Subfolder/SOOFile.xaml", UriKind.Absolute);

Table 4 illustrates the various relative pack URIs that you can specify in code using System..::.Uri.

Table 4: Relative Pack URIs in Code

File

Relative pack URI

Resource file - local assembly

Uri uri = new Uri("/ResourceFile.xaml", UriKind.Relative);

Resource file in subfolder - local assembly

Uri uri = new Uri("/Subfolder/ResourceFile.xaml", UriKind.Relative);

Resource file - referenced assembly

Uri uri = new Uri("/ReferencedAssembly;component/ResourceFile.xaml", UriKind.Relative);

Resource file in subfolder - referenced assembly

Uri uri = new Uri("/ReferencedAssembly;component/Subfolder/ResourceFile.xaml", UriKind.Relative);

Content file

Uri uri = new Uri("/ContentFile.xaml", UriKind.Relative);

Content file in subfolder

Uri uri = new Uri("/Subfolder/ContentFile.xaml", UriKind.Relative);

 

对于absolute Uri, 我们可以用Application.LoadComponent(resourceUri)方法读取

load的对象,返回值是一个object

对于relative Uri,Application.GetResourceStream(resourceUri) 方法读取

load的对象,返回值是一个Stream。对于xaml文件,就可以用XmlDocument读取这个

Stream了:

XmlDocument theXmlDocument = new XmlDocument();

                        theXmlDocument.Load(Stream);

你可能感兴趣的:(resource)