简体   繁体   English

如何在资源中使用外部Canvas XAML

[英]How to use external Canvas xaml in resource

I'm creating a wpf application in Visual Studio 2017. 我正在Visual Studio 2017中创建wpf应用程序。

I have some icons as separate files in xaml format. 我有一些图标作为xaml格式的单独文件。 They look something like this: 他们看起来像这样:

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_connect" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="36.7542" Height="36.7542" Canvas.Left="19.6229" Canvas.Top="19.6229" Stretch="Fill" Fill="#FF000000" Data="F1 M 25.7639,28.0031L 20.0866,22.3258C 19.4683,21.7075 19.4683,20.705 20.0866,20.0866C 20.705,19.4683 21.7075,19.4683 22.3258,20.0867L 28.0031,25.7639C 32.3443,22.5092 38.5302,22.856 42.4783,26.8042L 26.8041,42.4784C 22.856,38.5302 22.5092,32.3443 25.7639,28.0031 Z M 49.1958,33.5217C 53.144,37.4699 53.4908,43.6557 50.2361,47.9969L 55.9133,53.6742C 56.5317,54.2925 56.5317,55.295 55.9133,55.9134C 55.295,56.5317 54.2925,56.5317 53.6742,55.9134L 47.9969,50.2361C 43.6557,53.4908 37.4698,53.1441 33.5216,49.1959L 36.8804,45.8371L 34.0814,43.0381C 33.1539,42.1107 33.1539,40.6069 34.0814,39.6794C 35.0089,38.7519 36.5127,38.7519 37.4402,39.6794L 40.2392,42.4784L 42.4783,40.2392L 39.6794,37.4402C 38.7519,36.5127 38.7519,35.009 39.6794,34.0815C 40.6069,33.154 42.1106,33.154 43.0381,34.0815L 45.8371,36.8804L 49.1958,33.5217 Z "/>
</Canvas>

I notice they don't have an x:Key just an x:Name 我注意到他们没有x:Key只是x:Name

I have got them to work by copying & pasting the code into my resource library and adding an x:Key to reference them using 我通过将代码复制并粘贴到我的资源库中并添加一个x:Key来使用它们来使它们工作

<Viewbox>
    <ContentControl Content="{StaticResource appbar_connect}" />
</Viewbox>

in my main window xaml. 在我的主窗口xaml中。

But is there any way to just drop them into a folder and reference them by the name without having to open each one and copy & paste and add x:Tags? 但是,有什么方法可以将它们放到一个文件夹中并按名称引用它们,而不必打开每个文件夹并复制粘贴并添加x:Tags?

Not exactly what you was asking for but to manage my icons I have a ressourceDictonary containing all the icons as style and i select them using their x:Key like so: 不完全是您的要求,而是要管理我的图标,我有一个ressourceDictonary,其中包含所有图标作为样式,我使用它们的x:Key选择它们,如下所示:

</ResourceDictionary>
  <Style TargetType="{x:Type Path}" x:Key="Close">
    <Setter Property="Stroke" Value="Black"/>
    <Setter Property="Data" Value="M 15.125 12.28125 [...]Z"/>
  </Style>
  [...]
  <Style TargetType="{x:Type Path}" x:Key="Reload">
    <Setter Property="Stroke" Value="Black"/>
    <Setter Property="Data" Value="M 20 4 C 15.054688 4 11 [...]Z"/>
  </Style>
</ResourceDictionary>

Usage: 用法:

<Button>
   <Viewbox Height="25">
       <Path Fill="{StaticResource stdBackGround}" Style="{StaticResource Close}"/>
   </Viewbox>
</Button>  

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

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