如何正确重用 wpf 中的矢量图像 [英] How to reuse vector images in wpf properly
问题描述
我想在 WPF 中使用矢量图形.
I want to use vector graphic in WPF.
我有一堆 SVG 文件,我可以使用 Inkscape 将它们转换为 XAML.生成的 xaml 是 ResourceDictionary
和 ViewBox
/Canvas
包含 Path
等.字典被合并到 App 中.xaml
,我可以使用密钥访问它们.
I have bunch of SVG files, which I can convert into XAML by using Inkscape. Resulting xaml is ResourceDictionary
with ViewBox
/ Canvas
containing Path
, etc. Dictionaries are merged into App.xaml
and I can use key to access them.
问题:如何使用这样的图像?看起来我没有正确使用它们.
Question: how to use such images? It looks like I am not using them properly.
这是我如何使用它们
<Viewbox Child="{StaticResource MyImageResourceKey}" Width="100" Height="100"/>
但看起来我只能使用它一次(在一个地方)!尝试同时在多个位置使用该图像会将其从前一个位置移除或抛出
But it looks like I can use it only once (in one place)! Attempting to use that image in multiple places simultaneously will either remove it from previous place or will throw
System.ArgumentException: 在附加到新的父 Visual 之前,必须断开指定的子级与当前父 Visual 的连接.
System.ArgumentException: Must disconnect specified child from current parent Visual before attaching to new parent Visual.
你的问题
我想显示矢量图像列表.我这样显示它们
Y problem
I want to show a list of vector images. I display them like this
<ItemsControl ItemsSource="{Binding Images}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Viewbox Width="100" Height="100">
<ContentPresenter Content="{Binding Image}"/>
</Viewbox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
视图模型
public class ViewModelSomeWindow : INotifyPropertyChanged
{
public class MyImage
{
public object Image { get; set; }
}
private ObservableCollection<MyImage> _images;
public ObservableCollection<MyImage> Images
{
get { return _images; }
set { _images = value; OnPropertyChanged(); }
}
...
}
并且项目是这样添加的
Images.Add(new MyImage() { Image = App.Current.Resources["MyImageResourceKey"] });
问题:当第二项使用相同图像("MyImageResourceKey"
)时,第一项显示(空白)图像.如果图像已经在某处使用 StaticResource
显示,则添加项目将在 ArgumentException
上方抛出.
Problem: when using same image ("MyImageResourceKey"
) for second item, then first item displays (blank) image. If image is already displayed with the use of StaticResource
somewhere, then adding item will throw above ArgumentException
.
P.S.:我需要解决Y 问题
,但也许我没有正确使用矢量图形.
P.S.: I need to solve Y problem
, but perhaps I am not using vector graphic properly.
推荐答案
请在路径中使用x:shared =false,如附图所示
Please Use x:shared =false in path as shown in attached image
并访问此链接以获取参考
这篇关于如何正确重用 wpf 中的矢量图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!