Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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# 如何在Windows 8应用程序中将xaml矢量图像用作图像源_C#_Xaml_Windows 8_Vector Graphics_Inkscape - Fatal编程技术网

C# 如何在Windows 8应用程序中将xaml矢量图像用作图像源

C# 如何在Windows 8应用程序中将xaml矢量图像用作图像源,c#,xaml,windows-8,vector-graphics,inkscape,C#,Xaml,Windows 8,Vector Graphics,Inkscape,我在inkscape中创建了一些资产,并希望将它们用作windows 8应用程序中的图标。我读了一些书,发现尽管.NET4.5支持SVG。我使用 我得到以下xaml <Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="svg2997" Width="744.09448" Height="1052.3622" xmlns="http://schemas.microsoft.com/winfx/2006/

我在inkscape中创建了一些资产,并希望将它们用作windows 8应用程序中的图标。我读了一些书,发现尽管.NET4.5支持SVG。我使用

我得到以下xaml

<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="svg2997" Width="744.09448" Height="1052.3622" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Canvas x:Name="layer1">
    <Path Fill="#FFCCCCCC" Stroke="#FF000000" StrokeThickness="1.34377062" StrokeMiterLimit="4" x:Name="path3007" Data="M372.58272,134.72445C167.96301,134.72445 2.06820310000001,300.58818 2.06820310000001,505.20789 2.06820310000001,709.8276 167.96301,875.72241 372.58272,875.72241 577.20243,875.72241 743.06616,709.8276 743.06616,505.20789 743.06616,300.58818 577.20243,134.72445 372.58272,134.72445z M280.73888,251.77484L455.94149,251.77484 455.94149,413.70594 628.16035,413.70594 628.16035,588.97071 455.94149,588.97071 455.94149,773.71514 280.73888,773.71514 280.73888,588.97071 106.22005,588.97071 106.22005,413.70594 280.73888,413.70594 280.73888,251.77484z" />
  </Canvas>
</Canvas>


如果我将其直接添加到我的应用程序xaml中,它将渲染,但是比例太小了

如果可能的话,我想将其用作图像对象的图像源

<Image HorizontalAlignment="Left" Height="100" Margin="127,37,0,0" VerticalAlignment="Top" Width="100" Source="Assets/plus_circle.xaml"/>


这可以做到吗?

我很肯定,你不能仅仅将
路径
数据
注入
图像
中,并期望它神奇地工作,除非它通过一个对象。但是,您可以将
路径
采用到
内容控件
中,以相同的方式重复使用,而无需为每个实例绘制
对象

所以不是

<Image Source="..."/>
然后,在你的视野中,无论你在哪里,只要你愿意,就把它放在你的视野中

  <ContentControl Style="{StaticResource YourThingy}"/>


然而,你会想玩你的路径。它似乎设置了一个大尺寸,但希望这能为您的情况提供一个好的选择。干杯

大多数AppBar按钮基于标准样式中包含的样式,称为AppBarButtonStyle

要自定义按钮的文本,请设置AutomationProperties.Name附加属性;要自定义按钮中的图标,请设置Content属性;出于可访问性的原因,最好设置AutomationProperties.AutomationId附加属性

以下是使用此方法定制的按钮示例:

<Style x:Key="FolderButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
    <Setter Property="AutomationProperties.AutomationId" Value="FolderAppBarButton"/>
    <Setter Property="AutomationProperties.Name" Value="Folder"/>
    <Setter Property="Content" Value="&#xE188;"/>
</Style>

如上所述,要自定义图标,请设置内容属性。挑战在于如何设置内容,使其显示自定义矢量艺术

事实证明,您可以将任何路径Xaml(即使是您的路径)放置到视图框中以更改其比例。这是我的第一个方法,但它不起作用。事实上,似乎每当您使用Xaml扩展表示法为按钮设置Content属性时,它都不起作用

<Style x:Key="SquareButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="SquareAppBarButton"/>
<Setter Property="AutomationProperties.Name" Value="Square"/>
<Setter Property="Content">
    <Setter.Value>
        <!-- This square will never show -->
        <Rectangle Fill="Blue" Width="20" Height="20" />
    </Setter.Value>
</Setter>

我实际上认为这是一个bug,但幸运的是有一个解决方法

Tim Heuer写了一篇优秀的文章,介绍了使用Xaml路径作为按钮插图的最简单方法。该文章如下:

简而言之,您需要定义一种正确设置所有绑定的样式:

<Style x:Key="PathAppBarButtonStyle" BasedOn="{StaticResource AppBarButtonStyle}" TargetType="ButtonBase">
<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <Path Width="20" Height="20" 
                Stretch="Uniform" 
                Fill="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}" 
                Data="{Binding Path=Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
        </DataTemplate>
    </Setter.Value>
</Setter>

然后创建从该样式继承的样式,并粘贴到路径中。以下是您上面列出的艺术品的风格:

<Style x:Key="CrossButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource PathAppBarButtonStyle}">
    <Setter Property="AutomationProperties.AutomationId" Value="CrossAppBarButton"/>
    <Setter Property="AutomationProperties.Name" Value="Cross"/>
    <Setter Property="Content" Value="M372.58272,134.72445C167.96301,134.72445 2.06820310000001,300.58818 2.06820310000001,505.20789 2.06820310000001,709.8276 167.96301,875.72241 372.58272,875.72241 577.20243,875.72241 743.06616,709.8276 743.06616,505.20789 743.06616,300.58818 577.20243,134.72445 372.58272,134.72445z M280.73888,251.77484L455.94149,251.77484 455.94149,413.70594 628.16035,413.70594 628.16035,588.97071 455.94149,588.97071 455.94149,773.71514 280.73888,773.71514 280.73888,588.97071 106.22005,588.97071 106.22005,413.70594 280.73888,413.70594 280.73888,251.77484z"/>
</Style>

最后,在应用程序栏中使用它,如下所示:

<Button Style="{StaticResource CrossButtonStyle}" />

开发支持、设计支持和更多令人敬畏的优点:

包含图像元素的xaml元素是什么?如果从图像元素中删除宽度、高度和/或边距属性,纵横比会发生什么变化?层次结构是。不确定你所说的“纵横比”是什么意思?使用上面的代码,我根本无法显示图像。“如果我将其直接添加到我的应用程序xaml中,它将渲染,但是比例非常差。”-我在输入关于纵横比的短语。当我第一次读到你的问题时,我不清楚你在使用图像源时根本无法显示它-虽然标题中很清楚…你看到了吗?我看到了,但我猜它没有点击。谢谢你的帮助Nathan再次谢谢Jared。你总是在那里提醒我XAML有多棒。还有,我了解的太少了。我需要花更少的时间在JVS上,花更多的时间在XAML上。哇,从一开始我就不知道这和按钮有关哈哈洛,好观点克里斯W!我猜我做了一个假设,因为他说“图标”和图标通常用于按钮(尤其是AppBar按钮)。但同样的数据模板技巧可以应用于任何地方。通常你会看到人们使用Viewbox来缩放路径,但我的直觉告诉我Viewbox很昂贵。Tim Heuer展示的技巧看起来更轻。谢谢你的支持,帮助回答克里斯·W.的问题,我很感激。克里斯·G.肯定会运用一些疯狂的技巧来赢得比赛我认识的人已经赚大钱了!
<Button Style="{StaticResource CrossButtonStyle}" />