在我的 WPF 应用程序中,我加载的图像中的 PNG 徽标在设计时显示,但在运行时不显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15653105/
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
In my WPF application, my loaded PNG logo in image shows at design time but not at run time
提问by iAteABug_And_iLiked_it
This is probably something simple that I am missing. I have a png file which I want to use as the source of a *Image*control in my WPF window. I added this PNG file by Project Properties > Resources > Add Existing File and first as a linked file( and then as embedded when it didn't work).Then I add the *Source*for the image control in XAML file to this. No code involved, simple clicking.
这可能是我缺少的一些简单的东西。我有一个 png 文件,我想将其用作WPF 窗口中* Image* 控件的源。我通过“项目属性”>“资源”>“添加现有文件”添加了这个 PNG 文件,首先将其作为链接文件(然后在它不起作用时作为嵌入文件)。然后我将XAML 文件中图像控件的 * Source*添加到此文件。不涉及代码,简单的点击。
The annoying problem is that when I am designing the WPF window the image shows. When I run it , it doesnt. Nothing appears.
令人讨厌的问题是,当我设计 WPF 窗口时,图像会显示。当我运行它时,它没有。什么都没有出现。
Update: ADDED XAML CODE BELOW
更新:在下面添加了 XAML 代码
<Window x:Class="Server.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SERVER" Height="467.91" Width="620.522">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF080C59" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Button x:Name="btnConnect" Content="Connect" HorizontalAlignment="Left" Height="30" Margin="425,34,0,0" VerticalAlignment="Top" Width="134" Click="btnConnect_Click"/>
<Button x:Name="btnDisconnect" Content="Disconnect" HorizontalAlignment="Left" Height="35" Margin="425,69,0,0" VerticalAlignment="Top" Width="134" Click="btnDisconnect_Click"/>
<TextBlock x:Name="txtLog" HorizontalAlignment="Left" Margin="416,160,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="161" Width="87" Background="#FFFFF5F5" Text="LOG:"/>
<TextBox x:Name="txtMsg" HorizontalAlignment="Left" Height="23" Margin="416,326,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="112"/>
<Button x:Name="btnSend" Content="Send" HorizontalAlignment="Left" Height="35" Margin="425,120,0,0" VerticalAlignment="Top" Width="134" Click="btnSend_Click"/>
<ListView x:Name="lsvClients" Height="67" Margin="46,212,260,0" VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
<Image HorizontalAlignment="Left" Height="100" Margin="31,10,0,0" VerticalAlignment="Top" Width="101" Source="pack://siteoforigin:,,,/images/ServerMainLogo.png"/>
</Grid>
</Window>
What am I missing? thanks
我错过了什么?谢谢
回答by Clemens
When you specify the image URI in XAML, it is usually not necessary to write the full URI. Besides the full Pack URI shown in the other answer, you should also be able to write this:
在 XAML 中指定图像 URI 时,通常不需要编写完整的 URI。除了另一个答案中显示的完整 Pack URI 之外,您还应该能够这样写:
<Image ... Source="images/ServerMainLogo.png"/>
However, you have to make sure that the image file is located in a folder named imagesin your Visual Studio project and that its Build Actionis set to Resource, as shown in this answer.
但是,您必须确保图像文件位于imagesVisual Studio 项目中命名的文件夹中,并且其Build Action设置为Resource,如此答案中所示。
Alternatively you could set the Build Actionto Contentand Copy To
Output Directoryto Copy alwaysor Copy if newer. In this case the image is not embedded as resource into your program's assembly, but simply copied to a directory relative to the executable file.
或者,您可以将构建操作设置为Content并将复制到输出目录设置为Copy always或Copy if newer。在这种情况下,图像不会作为资源嵌入到程序的程序集中,而是简单地复制到与可执行文件相关的目录中。
The (relative) image URI in XAML would work in both cases.
XAML 中的(相对)图像 URI 在这两种情况下都适用。
回答by Rohit Vats
siteOfOriginshould be used only in case your file is copied at place where your otherexecutables(Output folder) resides. For Resources you should use applicationinstead.
siteOfOrigin仅当您的文件被复制到您的其他executables(输出文件夹)所在的位置时才应使用。对于资源,您应该application改用。
Source="pack://application:,,,/images/ServerMainLogo.png"
Refer to this link for further clarification Pack URIs.
请参阅此链接以进一步说明Pack URI。
回答by Ali Asad
- Make sure the image properties in the project has "Build Action" = "Resource", NOT "Embedded Resource"
In the xaml, with the image tag selected, use the properties windows to select the Source dropdown since NOW the image appears in the dropdown list! This let visual studio format the string for me. The string visual studio formatted
for my image was:
Source="pack://application:,,,/FamilyExplorer;component/Resources/Folder.png"/>
- 确保项目中的图像属性具有“Build Action”=“Resource”,而不是“Embedded Resource”
在 xaml 中,选择图像标记后,使用属性窗口选择源下拉列表,因为现在图像出现在下拉列表中!这让 Visual Studio 为我格式化字符串。格式化的字符串视觉工作室
我的形象是:
Source="pack://application:,,,/FamilyExplorer;component/Resources/Folder.png"/>
Where FamilyExplorerwas my project name and Resources/Folder.pngis the location of the image.
FamilyExplorer我的项目名称在哪里,Resources/Folder.png是图像的位置。

