WPF UserControls:即使使用'x:Shared =" False"',图片也会消失 [英] WPF UserControls: Image disappears even with 'x:Shared="False"'
问题描述
我在 ResourceDictionary
中为带有图像的按钮定义了一个样式:
I defined a style in a ResourceDictionary
for a button with an image:
<Style x:Key="BotonIrAInicioStyle" TargetType="Button">
<Setter Property="Margin" Value="0"/>
<Setter Property="Width" Value="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"/>
<Setter Property="Content">
<Setter.Value>
<Image Margin="2" Source="{StaticResource IconoDashboardBlanco}" MaxHeight="20" Stretch="Uniform"
RenderOptions.BitmapScalingMode="HighQuality"/>
</Setter.Value>
</Setter>
</Style>
图像源在另一个 ResourceDictionary
中定义在同一个程序集中,标记为 x:Shared =False
:
The image source is defined in another ResourceDictionary
in the same assembly, and marked as x:Shared="False"
:
<BitmapImage x:Key="IconoDashboardBlanco" x:Shared="False"
UriSource="pack://application:,,,/QualityFramework;component/Images/dashboard64X64.png"/>
由于样式将用于不同的程序集,我使用 pack:// application:,,,
表示引用图像。图像的 Build Action
设置为资源(不要复制到输出目录)
。
Since the style will be used in a different assembly, I used the "pack://application:,,,"
notation to reference the image. The Build Action
for the image is set to Resource (Do not copy to output directory)
.
在主程序集中,我有两个 UserControls
,它显示一个风格相同的按钮:
In the main assembly I have two UserControls
which display a button with identical style:
<Button DockPanel.Dock="Left" Style="{StaticResource BotonIrAInicioStyle}" Click="BotonIrAInicio_Click"/> (Click event has nothing to do with the problem)
问题:
我打开 UserControl A
包含带有图像的按钮,图像显示正常。然后我打开 UserControl B
包含一个相同的按钮,图像确定。我再次打开 UserControl A
,图像消失了。如果我打开 UserControl B
然后 UserControl A
,则会发生相同的情况,最后一个拥有图像。
I open UserControl A
containing the button with the image and the image is displayed ok. Then I open UserControl B
containing an identical button, image ok. I open UserControl A
again, and the image is gone. Happens the same if I open UserControl B
and then UserControl A
, the last one "owns" the image.
我到处都是,所有解决方案都指向 x:Shared =False
, URI表示法
和构建操作
...我应用了所有这些并且问题仍然存在。我也尝试过清洁和重建但没有成功。
I went everywhere and all the solutions point to the x:Shared="False"
, the URI notation
and the Build Action
... I applied all of them and the problem still happens. I also tried cleaning and rebuilding with no success.
我缺少什么?谢谢!
PS:如果我将两个按钮的内容直接设置为图像,它可以正常工作,但整个造型点就是避免这一点!
PS: if I set the content of both buttons to the image directly it works ok, but the whole point of styling is to avoid exactly that!
推荐答案
问题不在于 BitmapImage
它是按钮设置器中的内容 - 它创建一次,因此必须在实例之间跳转。
The problem is not the BitmapImage
it's the content in the setter of the button - it's created once and thus has to "jump" between instances.
简单但不是WPF-isque的解决方案是设置 x:共享样式上的假
。
The easy, but not WPF-isque, solution is setting x:Shared="False"
on the style.
正确的方法是使用 ControlTemplate
或 DataTemplate
。
根据您的观察:
如果我将两个按钮的内容直接设置到图像上就可以正常工作 - 这是因为这样做可以创建两个不同的 Image实例
对象。
"if I set the content of both buttons to the image directly it works ok" - this is because to do that you create two different instances of Image
object.
但是,这个:造型的全部要点就是避免错误!是一种误解 - 样式并不意味着设置内容控件的内容,c内容取决于上下文。如果你有一个重复所有按钮的视觉效果(不依赖于内容),它应该位于按钮的 ControlTemplate
中。如果您的视觉效果取决于内容(但内容不是视觉效果),则它应位于 DataTemplate
。
But, this: "the whole point of styling is to avoid exactly that!" is a misconception - styles are not meant to set the content of content controls, the content is dependent upon the context. If you have a visual that is repeating for all buttons (without dependency upon the content), it should reside in ControlTemplate
for the button. If you have a visual that is dependent upon the content (but the content is not the visual) it should reside in DataTemplate
.
这篇关于WPF UserControls:即使使用'x:Shared =" False"',图片也会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!