我可以使用 ViewBox 作为 WPF 中图像的来源吗?
Can I use a ViewBox as a Source for an Image in WPF?
我有一个在 XAML 中定义的图标,根元素是 Viewbox
。我想将其设置为 Image
元素的 Source
。
我试图将其直接设置为图像的来源,例如...
<Button Margin='5'>
<Image Source='{StaticResource IconKey}'></Image>
</Button>
但是我遇到了一个例外。有办法吗?
这对初学者来说是一个很大的主题,而且很棘手,因为有太多"stuff"你需要动脑筋。
我将尝试围绕该主题介绍一些内容。希望这可以减少您的困惑。
A Path 使用的数据看起来有点怪异 co-ordinate。这可以直接进入路径或几何体(或流几何体)。如果您使用几何图形,那么 re-usable 代表 red-whatever blue-whatever 等。它也不是视觉效果,因此您不会被视觉效果所吸引,只能拥有一个parent - 即在任何 ui.
中使用一次
我通常处理这类事情的方法是获得一个单一的几何图形。
通常不在 syncfusion metro studio(这是一个免费的库)之外,但有时我使用 inkscape(免费的)来跟踪位图或者我得到一个 svg 的路径。
但是您可以组合两个几何体。
在由 app.xaml 合并的资源字典中,您可以有一个几何:
<CombinedGeometry x:Key="TwoGeometriesInOne">
<CombinedGeometry.Geometry1>
<Geometry>
M3,1.6675c0,-0.1795..... and the rest of your first geometry data
</Geometry>
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<Geometry>
M15.9289,7.776L5.285,....
</Geometry>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
巨大的长字符串弄乱了网站,但是这两个几何图形应该只有 M... 到您为每条路径使用的数据的 z。
在按钮(或任何其他内容控件)中使用它
<Button Height="40" Width="40">
<Path Fill="#3E79B4" Data="{StaticResource TwoGeometriesInOne}"
Height="16"
Width="16"
Stretch="Fill"/>
</Button>
请注意,路径只能单独使用,而不是在 canvas 中使用,并且可以调整大小,也可以拉伸以适应它的大小或 parent 允许的任何大小。
另请考虑:
<Button Height="40" Width="40">
<Path Fill="#3E79B4" Data="{StaticResource TwoGeometriesInOne}"
Stretch="UniformToFill"
Margin="5"
/>
</Button>
路径有填充和描边。笔划是轮廓。如果你想在一个 "icon" 中使用不同颜色的形状,那么具有多个几何图形的绘图图像会更 suitable 因为每个几何图像都可以有自己的画笔。
我找到了替代方案。我没有在按钮内使用 Image
,而是使用了 Button
的 Content
属性。
<Button Content='{StaticResource IconKey}' />
我有一个在 XAML 中定义的图标,根元素是 Viewbox
。我想将其设置为 Image
元素的 Source
。
我试图将其直接设置为图像的来源,例如...
<Button Margin='5'>
<Image Source='{StaticResource IconKey}'></Image>
</Button>
但是我遇到了一个例外。有办法吗?
这对初学者来说是一个很大的主题,而且很棘手,因为有太多"stuff"你需要动脑筋。
我将尝试围绕该主题介绍一些内容。希望这可以减少您的困惑。
A Path 使用的数据看起来有点怪异 co-ordinate。这可以直接进入路径或几何体(或流几何体)。如果您使用几何图形,那么 re-usable 代表 red-whatever blue-whatever 等。它也不是视觉效果,因此您不会被视觉效果所吸引,只能拥有一个parent - 即在任何 ui.
中使用一次我通常处理这类事情的方法是获得一个单一的几何图形。 通常不在 syncfusion metro studio(这是一个免费的库)之外,但有时我使用 inkscape(免费的)来跟踪位图或者我得到一个 svg 的路径。
但是您可以组合两个几何体。
在由 app.xaml 合并的资源字典中,您可以有一个几何:
<CombinedGeometry x:Key="TwoGeometriesInOne">
<CombinedGeometry.Geometry1>
<Geometry>
M3,1.6675c0,-0.1795..... and the rest of your first geometry data
</Geometry>
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<Geometry>
M15.9289,7.776L5.285,....
</Geometry>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
巨大的长字符串弄乱了网站,但是这两个几何图形应该只有 M... 到您为每条路径使用的数据的 z。
在按钮(或任何其他内容控件)中使用它
<Button Height="40" Width="40">
<Path Fill="#3E79B4" Data="{StaticResource TwoGeometriesInOne}"
Height="16"
Width="16"
Stretch="Fill"/>
</Button>
请注意,路径只能单独使用,而不是在 canvas 中使用,并且可以调整大小,也可以拉伸以适应它的大小或 parent 允许的任何大小。
另请考虑:
<Button Height="40" Width="40">
<Path Fill="#3E79B4" Data="{StaticResource TwoGeometriesInOne}"
Stretch="UniformToFill"
Margin="5"
/>
</Button>
路径有填充和描边。笔划是轮廓。如果你想在一个 "icon" 中使用不同颜色的形状,那么具有多个几何图形的绘图图像会更 suitable 因为每个几何图像都可以有自己的画笔。
我找到了替代方案。我没有在按钮内使用 Image
,而是使用了 Button
的 Content
属性。
<Button Content='{StaticResource IconKey}' />