Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中使用资源字典_C#_.net_Wpf - Fatal编程技术网

C# 如何在WPF中使用资源字典

C# 如何在WPF中使用资源字典,c#,.net,wpf,C#,.net,Wpf,我是WPF新手,不太了解资源字典的工作原理。我有Icons.xaml,看起来像: <ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Canvas x:Key="appbar_3d_3ds" Width="76" H

我是WPF新手,不太了解资源字典的工作原理。我有Icons.xaml,看起来像:

<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas x:Key="appbar_3d_3ds" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="32" Height="40" Canvas.Left="23" Canvas.Top="18" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 27,18L 23,26L 33,30L 24,38L 33,46L 23,50L 27,58L 45,58L 55,38L 45,18L 27,18 Z "/>
</Canvas>

<Canvas x:Key="appbar_3d_collada" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="44" Height="30.3735" Canvas.Left="15" Canvas.Top="21.6194" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 39.2598,21.6194C 47.9001,21.6194 55.3802,24.406 59,28.4646L 59,33.4834C 56.3537,29.575 49.2267,26.7756 40.85,26.7756C 30.2185,26.7756 21.6,31.285 21.6,36.8475C 21.6,40.4514 25.2176,43.6131 30.6564,45.3929C 22.7477,43.5121 17.2,39.1167 17.2,33.9944C 17.2,27.1599 27.0765,21.6194 39.2598,21.6194 Z M 35.8402,51.9929C 27.1999,51.9929 19.7198,49.2063 16.1,45.1478L 15,40.129C 17.6463,44.0373 25.8733,46.8367 34.25,46.8367C 44.8815,46.8367 53.5,42.3274 53.5,36.7648C 53.5,33.161 49.8824,29.9992 44.4436,28.2194C 52.3523,30.1002 57.9,34.4956 57.9,39.6179C 57.9,46.4525 48.0235,51.9929 35.8402,51.9929 Z "/>
</Canvas>
</ResourceDictionary>


如何在xaml中使用例如“app_3d_collada”?例如,我有MenuItem,我想使用此图标作为我的MenuItem图标。

首先,您必须在App.xaml文件中指定资源字典引用

在App.xaml文件中的Application.Resources标记下

<ResourceDictionary Source="Icons.xaml" />

资源字典的源可以根据放置资源字典的路径而有所不同

现在,在任何窗口或页面xaml文件中,您都可以引用以下样式/图标

<MenuItem Header="Reports" Icon="{StaticResource app_3d_collada}">
</MenuItem>


风格也是如此。希望这能解决问题。

可能重复@GauravP,该解释假设OP了解web。没有标记asp.net。@GauravP它在CSS中不同,我只使用class=”“或id=“”,这里我必须使用{StaticResource keyname}是否可以设置我的图标的大小?是的,您已经在app_3d_collada key的canvas标记下的icons.xaml文件中进行了设置。您可以在此处更改以设置图标的大小。如果我的图标显示在文本下方,即使我将大小设置为10x10,它也位于菜单项名称下方。伟大的这对我也有帮助。