如何使用 XamlReader 从程序集中的 Xaml 文件加载? [英] How does use XamlReader to load from a Xaml file from within the assembly?

查看:30
本文介绍了如何使用 XamlReader 从程序集中的 Xaml 文件加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 stackoverflow 和互联网上的其他地方找到了几篇关于如何从静态文件加载 Xaml 的帖子:他们建议创建一个 XmlReader 或 StreamReader 指向在文件系统上找到的文件,但 .xaml 文档我想要读取的内容将与程序集的其余部分一起编译,因此它不会有一个有意义的文件 Uri.我不想在程序集去的任何地方复制这个文件.有没有办法读取已编译到程序集中的 .xaml 文档?

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. 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 生成的 UIElement 应该很容易编辑,我通过在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.

为了说明我的期望,这里有一个例子:

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.

推荐答案

在您可以从程序集中加载 Xaml 作为嵌入式资源之前,您必须进行一些设置.我将向您介绍一个示例,然后您可以根据自己的需要对其进行自定义.

Before you can load Xaml from an assembly as an embedded resource, there is a bit of setup you must do. I'll walk you through an example, then from there you can customize it to suite your needs.

  1. 在您的项目中创建文件夹.将其命名为 XAML.
  2. 将 XAML 文件添加到 XAML 文件夹.我们称之为 Sample.xaml.
  3. 右键单击 Sample.Xaml 并选择属性.将 Build Action 的值设置为Embedded Resource".
  4. 右键单击项目并选择属性.记下默认命名空间值.我们将使用它作为路径的一部分.对于这个例子,假设它是MyNamespace.

加载 Xaml 资源的代码如下所示:

Your code to load the Xaml resource would look something like this:

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);
}

如您所见,资源的路径由项目的默认命名空间、文件的文件夹路径和文件名组成.如果文件夹路径有多个级别,请使用点作为文件夹分隔符代替反斜杠.例如 Xaml\Subfolder 将是 Xaml.Subfolder.

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.

这篇关于如何使用 XamlReader 从程序集中的 Xaml 文件加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆