在我的 WPF 应用程序中,我加载的图像中的 PNG 徽标在设计时显示,但在运行时不显示 [英] In my WPF application, my loaded PNG logo in image shows at design time but not at run time

查看:28
本文介绍了在我的 WPF 应用程序中,我加载的图像中的 PNG 徽标在设计时显示,但在运行时不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是我遗漏的一些简单的东西.我有一个 png 文件,我想将其用作 WPF 窗口中 *Image* 控件的源.我通过 Project Properties > Resources > Add Existing File 添加了这个 PNG 文件,首先作为链接文件(然后在它不起作用时嵌入).然后我为图像添加 *Source*XAML 文件中的控制.不涉及代码,简单点击.

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.

烦人的问题是,当我设计 WPF 窗口时,图像会显示.当我运行它时,它没有.什么都没有出现.

The annoying problem is that when I am designing the WPF window the image shows. When I run it , it doesnt. Nothing appears.

更新:在下面添加了 XAML 代码

Update: ADDED XAML CODE BELOW

<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>

我错过了什么?谢谢

推荐答案

在 XAML 中指定图像 URI 时,通常不需要编写完整的 URI.除了另一个答案中显示的完整 Pack URI 之外,您还应该可以这样写:

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:

<Image ... Source="images/ServerMainLogo.png"/>

但是,您必须确保图像文件位于 Visual Studio 项目中名为 images 的文件夹中,并且它的 Build Action 设置为 Resource,如 这个答案.

However, you have to make sure that the image file is located in a folder named images in your Visual Studio project and that its Build Action is set to Resource, as shown in this answer.

或者,您可以将 Build Action 设置为 ContentCopy To输出目录Copy alwaysCopy if newer.在这种情况下,图像不会作为资源嵌入到程序的程序集中,而只是复制到与可执行文件相关的目录中.

Alternatively you could set the Build Action to Content and Copy To Output Directory to Copy always or 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.

XAML 中的(相对)图像 URI 在这两种情况下都适用.

The (relative) image URI in XAML would work in both cases.

这篇关于在我的 WPF 应用程序中,我加载的图像中的 PNG 徽标在设计时显示,但在运行时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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