Kyl*_* V. 6 c# wpf xaml visual-studio-2012
我下载了Visual Studio图像库,我看到包含的图标以.XAML文件的形式提供矢量格式.这是一个例子:
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>
Run Code Online (Sandbox Code Playgroud)
我想将我想要在我的应用程序源中使用的所有图标xaml文件放在一个名为"Icons"的文件夹中,然后能够在IconDictionary.xaml其他地方调用一个文件来定义一个ResourceDictionary持有a MergedDictionaries并在其中MergedDictionaries我希望能够包含icon.xaml文件以某种方式为它们分配一个x:Key属性,所以我可以在整个应用程序中将它们称为静态资源.
是否可以使用此icon.xaml文件而无需修改.xaml文件本身?
我想能够刚刚离开他们,因为他们是可惜好像我别无选择,只能要么其内容复制到我的IconDictionary.xaml或编辑单个的.xaml文件,并围绕他们的ResourceDictionary一个x:Key,我可以添加到我的MergedDictionaries.
UPDATE
为了清楚起见,我希望我的IconDictionary.xaml看起来像这样:
<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>
Run Code Online (Sandbox Code Playgroud)
是否可以在不修改 .xaml 文件本身的情况下使用此 icon.xaml 文件?
我不相信。虽然XAML看起来很像XML,并遵循了大量的XML规则,在XAML编译器似乎并没有让DOCTYPE和ENTITY标记通常会被用来导入XML从一个文件到另一个。使用时,报告的第一个错误为“此 XML 文档中禁止使用 DTD”。如果无法提供 DTD,就无法声明实体,因此将无法导入独立的 XAML。
如果您想直接使用生成的 XAML 文件,您可以将该文件作为资源添加到您的项目中。然后可以XamlReader.Load()在代码隐藏中使用资源数据加载,在您需要的适当时间添加。
例如,假设您已将 XAML 文件复制到项目中名为“Resources”的文件夹中,并将文件的“Build Action”设置为“Resource”,您可以编写如下代码来检索对象:
using (Stream stream = App.GetResourceStream(
new Uri("pack://application:,,,/Resources/Add_16x.xaml")).Stream)
{
object o = XamlReader.Load(stream);
}
Run Code Online (Sandbox Code Playgroud)
这将获得一个Stream表示已嵌入应用程序的 XAML 文件数据的对象,然后使用该XamlReader.Load()方法加载 XAML 表示的 WPF 对象。
就我个人而言,当我使用这些文件时,我只是将DrawingGroupXAML 文件中的元素复制/粘贴到我自己的资源 XAML 文件中。无论如何,包装器 ( Viewbox, Rectangle, 和DrawingBrush) 都是多余的。我真正需要的是DrawingGroup对象。
当我这样做时,我还删除了显式<DrawingGroup.Drawing>语法,并直接包含 的子项DrawingGroup,使 XAML 更简洁一些。例如:
<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>
Run Code Online (Sandbox Code Playgroud)
然后,您可以随意使用上述资源。例如,WPF 中的惯用方法是声明一个DataTemplate配置为Drawing以您想要的方式显示一个,然后将给定的Drawing资源绑定为内容控件的内容。例如:
<DataTemplate DataType="{x:Type Drawing}">
<Rectangle>
<Rectangle.Fill>
<DrawingBrush Drawing="{Binding}" Stretch="Uniform"/>
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
然后在别处:
<Button Content="{StaticResource Add_16x}"/>
Run Code Online (Sandbox Code Playgroud)
(确保内容控件的样式,例如您的Button,与您的Drawing资源兼容,即正确的大小等)
| 归档时间: |
|
| 查看次数: |
1272 次 |
| 最近记录: |