WPF - 图像未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22584072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - Image not showing up
提问by coolerfarmer
I have my application and now I want to add a close button. I created it in the designer with a Image (Maybe there is a better way! If so, please answer) and everything seems to work:

我有我的应用程序,现在我想添加一个关闭按钮。我在设计器中用图像创建了它(也许有更好的方法!如果是这样,请回答)并且一切似乎都有效:

Then I run the application and it looks like this:
然后我运行应用程序,它看起来像这样:


Why isn't there the button / Image anymore? I have no idea! BTW, the picture is a .png and is saved in the resources!
为什么不再有按钮/图像?我不知道!顺便说一句,图片是.png,保存在资源中!
my wpf code:
我的 wpf 代码:
<Window x:Class="Chat_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="1000" WindowStyle="None" AllowsTransparency="True">
<Window.Background>
<RadialGradientBrush>
<GradientStop Color="#FF323A44" Offset="1"/>
<GradientStop Color="#FF384A5A"/>
</RadialGradientBrush>
</Window.Background>
<Grid>
<Border BorderThickness="1" Height="26" VerticalAlignment="Top" Background="#FF436B85" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<Image HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="33" Source="pack://siteoforigin:,,,/Resources/simat_btn_chat.png" MouseLeftButtonDown="Image_MouseLeftButtonDown" Margin="963,2,0,0"/>
</Border>
</Grid>
</Window>
BTW, I tried it with a ico file too!
顺便说一句,我也用 ico 文件试过了!
Thank you!
谢谢!
回答by Sajeetharan
Remove the verticalAlighment and horizontalAlignment of image and wrap it inside a grid, It should be like this,
把图片的verticalAlighment和horizontalAlignment去掉,包裹在一个grid里面,应该是这样的,
<Border BorderThickness="1" Height="26" VerticalAlignment="Top" Background="#FF436B85">
<Grid HorizontalAlignment="Right" Width="24">
<Image Source="Images/download.jpg" Margin="0,0,0,0" d:LayoutOverrides="Width"/>
</Grid>
</Border>


回答by ryrich
try adding it as a resource (enables reusability too!)
尝试将其添加为资源(也可以实现可重用性!)
<Window.Resources>
<Image x:Key="MyImage" Source.../>
</Window.Resources>
Then use it in your Grid as:
然后在您的网格中使用它作为:
<Button Content={StaticResource MyImage} />
Also, make sure your image is built as a Resource
另外,请确保您的图像构建为 Resource
EDIT:The button is showing up, but not your image. So it cannot find it properly. Try changing your pack url.
编辑:按钮显示,但不是您的图像。所以它无法正确找到它。尝试更改您的包装网址。
Either like,
要么喜欢,
Source="pack://application:,,,/Resources/simat_btn_chat.png"
or
或者
Source="/Resources/simat_btn_chat.png"

