在WPF中消失的图标 [英] Disappearing icons in WPF

查看:253
本文介绍了在WPF中消失的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在WPF应用程序中使用VS2008ImageLibrary/Actions/pngformat中的一些图标来获得更直观的Visual Studio外观-类似于WinForms中的插入标准项"命令. 我已经将图像添加为资源,并在我的xaml文件中引用了它们:

I'm looking to use some icons from the VS2008ImageLibrary/Actions/pngformat in my WPF application to get a more Visual Studio look and feel - like the "Insert Standard Items" command in WinForms.
I've added the images as resources, and referenced them in my xaml file:

<DockPanel.Resources>
        <Image x:Key="NewIcon" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16"/>
</DockPanel.Resources>

然后我将图像添加到了菜单和工具栏

And then I've added the image to the Menu and Toolbar

<MenuItem Command="ApplicationCommands.New" Icon="{StaticResource NewIcon}"/>
<!-- ... -->
<Button Command="ApplicationCommands.Save"><StaticResourceExtension ResourceKey="SaveIcon"/></Button>

但是,当我打开菜单时,图标从工具栏上消失了,好像按钮从没有消失.我在其他地方读到这种情况是由于.NET缺少对Vista图标/压缩PNG的支持而引起的,但我不确定这是否是完全相同的问题.我通过更改格式来解决此问题的尝试失败,或者给出了次优的结果(例如失去透明度). 有人知道更好的方法吗?

However, when I open the menu, the icon disappears from the Toolbar, as if the button was never there.I've read elsewhere that this kind of thing occurs due to .NET's lack of support for Vista Icons/Compressed PNGs, but I'm not sure this is exactly the same problem. My attempts to work around this by changing formats have failed, or given sub-optimal results (such as losing transparency). Does anyone know of a better way to do this?

对于其他遇到此问题的人,请在<中使用不同的键重复输入.每个引用的DockPanel.Resources>标签似乎是一种功能足够的解决方法,尽管我担心如果由于任何原因需要重新加载图像,它们可能会再次消失.

For other people having this issue, making duplicate entries with different keys in the < DockPanel.Resources > tag for each reference seems to be a functional enough workaround, though I'd be concerned that the images might disappear again if they need to be reloaded for any reason.

推荐答案

我认为您必须定义一个单独的资源以供Button使用:

I think you have to define a separate resource for usage with the Button:

    <DockPanel.Resources>
        <Image x:Key="NewIconForMenu" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16"/>
<Image x:Key="NewIconForButton" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16"/>
</DockPanel.Resources>

我想,当您声明一个Image资源时,它基本上是在创建Image类型的对象.之后,您可以通过设置Icon和Content属性使其成为MenuItem和Button的子级,因此它不再是一棵树.这是不合法的.

I suppose, when you declare an Image resource it is basically creating object of type Image. After that you make it a child of the MenuItem and the Button by setting Icon and Content properties, therefore it is not a tree anymore. This is not legal.

如果您尝试声明第二个也使用NewIcon的按钮,则会得到逻辑树的异常.

If you try to declare the second button that also uses NewIcon, you'll get an exception of logical tree.

我不知道MenuItem如何逃脱该异常,可能是在将图像添加到自己的逻辑子级集合之前,从Button的子级中删除Image元素(这就是它的消失方式)足够聪明.

I don't know how MenuItem escaped that exception, may be it is smart enough to remove the Image element from Button's children (this is how it disappears) before it adds the image to own logical children collection.

好的,我刚刚找到了一个更简单的解决方案.您可以在字典中的图像声明中添加x:Shared ="false".这样可以确保创建该图像的新实例,并且不会破坏wpf树规则.

Okay, I just found a simpler solution. You could add x:Shared="false" to the image declaraiton in the dictionary. This will ensure that a new instance of that image is created and it would not break wpf tree rules.

这篇关于在WPF中消失的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆