简体   繁体   English

如何使用XamlReader从程序集中的Xaml文件加载?

[英]How does use XamlReader to load from a Xaml file from within the assembly?

I've found several posts across stackoverflow and the rest of the internet regarding how to load Xaml from a static file: they recommend creating a XmlReader or StreamReader pointing to a file found on the file system, but the .xaml document I would like to read from is going to be compiled with the rest of the assembly, so it won't have a meaningful file Uri. 我已经在stackoverflow和互联网的其余部分找到了一些有关如何从静态文件加载Xaml的文章:他们建议创建一个XmlReader或StreamReader指向文件系统上找到的文件,但是我想要的.xaml文档read from将与程序集的其余部分一起编译,因此它将没有有意义的文件Uri。 I do not want to copy this document around wherever the assembly goes. 我不想在程序集所在的任何地方复制此文档。 Is there a way to read from a .xaml document that has been compiled into the assembly? 有没有办法从已编译到程序集中的.xaml文档中进行读取?

I also know that I can simply read from a very long string literal inside the code itself, but I'd rather not do that - the UIElement produced from the Xaml should be easily edited, and I gain this by editing it in a Xaml file. 我也知道我可以从代码本身内部的一个很长的字符串文字中读取内容,但是我不愿意这样做-Xaml产生的UIElement应该易于编辑,而我可以通过在Xaml文件中对其进行编辑来实现。

To illustrate what I'm hoping for, here's an example: 为了说明我的期望,下面是一个示例:

private void LoadUIElementFromCompiledXaml()
{
    XmlReader xmlReader = new XmlReader("*Uri for .xaml document within my assembly*");
    UIElement elementLoaded = (UIElement)XamlReader.Load(xmlReader);
}

I apologize in advance if the answer is blatantly obvious. 如果答案很明显,我预先表示歉意。

Before you can load Xaml from an assembly as an embedded resource, there is a bit of setup you must do. 在可以从程序集中将Xaml作为嵌入式资源加载之前,必须进行一些设置。 I'll walk you through an example, then from there you can customize it to suite your needs. 我将带您看一个示例,然后从那里可以自定义它以满足您的需求。

  1. Create folder in your project. 在您的项目中创建文件夹。 Name it XAML. 将其命名为XAML。
  2. Add a XAML file to the XAML folder. 将XAML文件添加到XAML文件夹。 Lets call it Sample.xaml. 让我们将其称为Sample.xaml。
  3. Right-click on Sample.Xaml and choose properties. 右键单击Sample.Xaml并选择属性。 Set the value for Build Action to "Embedded Resource". 将“生成操作”的值设置为“嵌入式资源”。
  4. Right-click on the project and choose properties. 右键单击项目,然后选择属性。 Take note of the Default namespace value. 注意默认名称空间值。 We will use this as part of the path. 我们将使用它作为路径的一部分。 For this example lets assume it is "MyNamespace. 对于此示例,假设它是“ MyNamespace。

Your code to load the Xaml resource would look something like this: 您用于加载Xaml资源的代码如下所示:

string defaultNamespace = "MyNamespace";
string folderName = "XAML";
string fileName = "Sample.xaml";

string path = String.Format("{0}.{1}.{2}", defaultNamespace, folderName, fileName);

using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(path))        
{
    object root = XamlReader.Load(stream);
}

As you can see the path to the resource is made up of the default namespace of the project, the folder path to the file, and the file name. 如您所见,资源的路径由项目的默认名称空间,文件的文件夹路径以及文件名组成。 If the folder path has multiple levels use dots as folder separator in place of back slashes. 如果文件夹路径具有多个级别,请使用点作为文件夹分隔符来代替反斜杠。 For example Xaml\\Subfolder would be Xaml.Subfolder. 例如,Xaml \\ Subfolder将是Xaml.Subfolder。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM