将单独的XAML导入为ResourceDictionary并分配x:Key [英] Import separate XAML as ResourceDictionary and assign x:Key

查看:65
本文介绍了将单独的XAML导入为ResourceDictionary并分配x:Key的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了 Visual Studio图像库,看到包括的图标以.XAML文件形式的矢量格式可用.这是一个示例:

I downloaded the Visual Studio Image Library and I see that the included icons are available in vector format in the form of .XAML files. Here is an example:

Add_16x.xaml

<!-- This file was generated by the AiToXaml tool.-->
<!-- Tool Version: 14.0.22307.0 -->
<Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Rectangle Width="16" Height="16">
    <Rectangle.Fill>
      <DrawingBrush>
        <DrawingBrush.Drawing>
          <DrawingGroup>
            <DrawingGroup.Children>
              <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
              <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M5.0004,-0.000199999999999534L5.0004,4.9998 0.000399999999999956,4.9998 0.000399999999999956,10.9998 5.0004,10.9998 5.0004,15.9998 10.9994,15.9998 10.9994,10.9998 16.0004,10.9998 16.0004,4.9998 10.9994,4.9998 10.9994,-0.000199999999999534z" />
              <GeometryDrawing Brush="#FF388A34" Geometry="F1M10,6L15,6 15,10 10,10 10,15 6,15 6,10 1,10 1,6 6,6 6,1 10,1z" />
            </DrawingGroup.Children>
          </DrawingGroup>
        </DrawingBrush.Drawing>
      </DrawingBrush>
    </Rectangle.Fill>
  </Rectangle>
</Viewbox>

我想将要在我的应用程序源中使用的所有图标xaml文件放在一个名为"Icons"的文件夹中,然后能够将一个名为 IconDictionary.xaml 的文件放在其他定义的位置一个 ResourceDictionary 持有一个 MergedDictionaries ,并且在此 MergedDictionaries 中,我希望能够以某种方式包括icon.xaml文件并为其分配一个x:Key 属性,因此我可以在整个应用程序中将它们称为静态资源.

I want to put all the icon xaml files I want to use in my application source in a folder called "Icons" and then be able to have a file called IconDictionary.xaml somewhere else that defines a ResourceDictionary holding a MergedDictionaries and inside this MergedDictionaries I want to be able to include the icon.xaml files somehow and assign them an x:Key property so I can refer to them throughout my application as static resources.

是否可以在不修改.xaml文件本身的情况下使用此icon.xaml文件?

我希望能够直接保留它们,但不幸的是,我似乎别无选择,只能将其内容复制到我的 IconDictionary.xaml 或编辑单个.xaml文件并用 x:Key 将它们包围在 ResourceDictionary 中,我可以将其添加到我的 MergedDictionaries 中.

I would like to be able to just leave them as they are but unfortunately it seems like I have no choice but to either copy their contents into my IconDictionary.xaml or edit the individual .xaml files and surround them in a ResourceDictionary with a x:Key that I could add to my MergedDictionaries.

更新

为了清晰起见,这是我想要的IconDictionary.xaml:

Here is what I would like my IconDictionary.xaml to look like for clarity:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <DataTemplate x:Key="Add_16x">
        <!-- Import Viewbox from Add_16x.xaml here... -->
    </DataTemplate>

</ResourceDictionary>

推荐答案

是否可以在不修改.xaml文件本身的情况下使用此icon.xaml文件?

Is it possible to use this icon.xaml file without modifying the .xaml file itself?

我不这么认为.尽管XAML看起来很像XML,并且遵循许多XML规则,但XAML编译器似乎不允许通常使用的 DOCTYPE ENTITY 标记将XML从一个文件导入另一个文件.使用时,报告的第一个错误为此XML文档中禁止使用DTD" .没有提供DTD的能力,您将无法声明实体,因此将无法导入独立的XAML.

I don't believe so. While XAML looks a lot like XML, and follows a lot of XML rules, the XAML compiler doesn't seem to allow the DOCTYPE and ENTITY markup that would normally be used to import XML from one file into another. When used, the first error reported reads "DTD is prohibited in this XML document". Without the ability to provide a DTD, you can't declare entities, and thus won't be able to import the standalone XAML.


如果要直接使用生成的XAML文件,则可以将该文件作为资源添加到项目中.然后可以使用 XamlReader.Load()(在代码后面)加载资源数据,并在适当的时间添加它.


If you want to use the generated XAML file directly, you can add the file to your project as a Resource. The resource data can then be loaded using XamlReader.Load() in code-behind, added at the appropriate time where you want it.

例如,假设您已将XAML文件复制到项目中名为资源" 的文件夹中,并将文件的构建操作" 设置为资源" ,您可以编写如下代码来检索对象:

For example, assuming you have copied the XAML file to a folder named "Resources" in your project, and set the "Build Action" for the file to "Resource", you can write code like this to retrieve the object:

using (Stream stream = App.GetResourceStream(
    new Uri("pack://application:,,,/Resources/Add_16x.xaml")).Stream)
{
    object o = XamlReader.Load(stream);
}

这将得到一个 Stream 对象,该对象表示已嵌入在应用程序中的XAML文件数据,然后使用 XamlReader.Load()加载XAML表示的WPF对象.code>方法.

That will get a Stream object representing the XAML file data that has been embedded in your application and then load the WPF object the XAML represents, using the XamlReader.Load() method.


就个人而言,当我使用这些文件时,我只是将Xcode文件中的 DrawingGroup 元素复制/粘贴到我自己的资源XAML文件中.无论如何,包装器( Viewbox Rectangle DrawingBrush )都是多余的.我真正需要的是 DrawingGroup 对象.


Personally, when I am using these files, I just copy/paste the DrawingGroup element from the XAML file, into my own resource XAML file. The wrapper (Viewbox, Rectangle, and DrawingBrush) are all redundant anyway. What I really need is the DrawingGroup object.

执行此操作时,我还删除了显式的< DrawingGroup.Drawing> 语法,并直接包含了 DrawingGroup 的子代,这使得XAML有点更简洁.例如:

When I do this, I also remove the explicit <DrawingGroup.Drawing> syntax, and just include the children of the DrawingGroup directly, making the XAML a bit more concise. E.g.:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <DrawingGroup x:Key="Add_16x">
    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M5.0004,-0.000199999999999534L5.0004,4.9998 0.000399999999999956,4.9998 0.000399999999999956,10.9998 5.0004,10.9998 5.0004,15.9998 10.9994,15.9998 10.9994,10.9998 16.0004,10.9998 16.0004,4.9998 10.9994,4.9998 10.9994,-0.000199999999999534z" />
    <GeometryDrawing Brush="#FF388A34" Geometry="F1M10,6L15,6 15,10 10,10 10,15 6,15 6,10 1,10 1,6 6,6 6,1 10,1z" />
  </DrawingGroup>
</ResourceDictionary>

然后您可以根据需要使用以上资源.例如,WPF中的一种惯用方法是声明一个 DataTemplate ,配置为以所需方式显示 Drawing ,然后绑定给定的 Drawing 资源作为内容控件的内容.例如:

You can then use the above resource however you like. For example, an idiomatic approach in WPF would be to declare a DataTemplate configured to display a Drawing the way you want, and then bind a given Drawing resource as the content for a content control. E.g.:

<DataTemplate DataType="{x:Type Drawing}">
  <Rectangle>
    <Rectangle.Fill>
      <DrawingBrush Drawing="{Binding}" Stretch="Uniform"/>
    </Rectangle.Fill>
  </Rectangle>
</DataTemplate>

然后在其他地方:

<Button Content="{StaticResource Add_16x}"/>

(请确保内容控件的样式(例如您的 Button )与您的 Drawing 资源(即正确的尺寸等)兼容"

(Making sure the style for the content control, e.g. your Button, is otherwise compatible with your Drawing resource, i.e. correct size, etc.)

这篇关于将单独的XAML导入为ResourceDictionary并分配x:Key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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