【问题标题】:How to use external Canvas xaml in resource如何在资源中使用外部 Canvas xaml
【发布时间】:2019-06-25 00:32:30
【问题描述】:

我正在 Visual Studio 2017 中创建一个 wpf 应用程序。

我有一些图标作为 xaml 格式的单独文件。它们看起来像这样:

<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>

我注意到他们没有 x:Key 只是一个 x:Name

我通过将代码复制并粘贴到我的资源库中并添加 x:Key 以使用它们引用它们来让它们工作

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

在我的主窗口 xaml 中。

但是有什么方法可以将它们放到一个文件夹中并通过名称引用它们,而不必打开每个并复制粘贴并添加 x:Tags?

【问题讨论】:

    标签: c# wpf xaml visual-studio-2017


    【解决方案1】:

    不完全是您所要求的,但为了管理我的图标,我有一个 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>
    

    用法:

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

    【讨论】:

    • 谢谢,这似乎是一种比我目前更好的做事方式,并且还可以让您覆盖颜色。不过仍然有很多复制和粘贴代码。
    • 对不起,我在这里做错了,我更新了我的回复!当然,您不复制数据而只使用样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多