C# WPF 按钮中的图像在运行时不可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15636047/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:26:19  来源:igfitidea点击:

Image in WPF Button not Visible at Runtime

c#wpfimagebutton

提问by MoonKnight

All, I have the following start to a small application that checks .resx files for consistency of embedded brackets (so that runtime errors of non-matching "... {0}" strings don't happen). I have the following XAML for the MainWindow.xaml, and my particular problem relates to the image that is to be displayed on the button

所有,我有一个小应用程序的以下开始,该应用程序检查 .resx 文件的嵌入式括号的一致性(以便不发生不匹配的“... {0}”字符串的运行时错误)。我有 MainWindow.xaml 的以下 XAML,我的特殊问题与要显示在按钮上的图像有关

<Window x:Class="ResxChecker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="174.383" Width="495.869">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="350*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="30*"/>
        </Grid.RowDefinitions>
        <Label Content="Select .resx file:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="24" Width="Auto" Grid.ColumnSpan="1"/>
        <TextBox Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
        <Button Grid.Column="2" HorizontalAlignment="Right" Margin="5,0,10,0" Grid.Row="1">
            <Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="16 " Width="16" Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"/>
        </Button>
    </Grid>
</Window>

The image has 'Build Action = Resource', 'Copy to output directory = Do not copy' - the image shows in the designer but not at runtime. I have seen the following questions and read the relevant answers, but none resolve the problem:

图像具有“构建操作 = 资源”、“复制到输出目录 = 不复制” - 图像显示在设计器中但不在运行时显示。我看到了以下问题并阅读了相关答案,但没有一个能解决问题:

  1. WPF control images not displayed when consumed by an application

  2. image problem in wpf (image does not show up)

  3. Background Image of Button not showing in WPF

  1. 应用程序使用时不显示 WPF 控件图像

  2. wpf中的图像问题(图像不显示)

  3. WPF 中未显示按钮的背景图像

How do I get the button image to appear at runtime?

如何让按钮图像在运行时出现?

采纳答案by Phil

Change the build action to 'Resource'. Also your pack url is wrong. Either use:

将构建操作更改为“资源”。你的包网址也是错误的。要么使用:

Source="pack://application:,,,/Resource/UserCost2013Open16.png"

or simply

或者干脆

Source="/Resource/UserCost2013Open16.png"

回答by Basti

There are 2 Solutions:

1: Change the settings of the image:

有2种解决方案:

1:更改图像的设置:

Build Action = Content
Copy to output directory = Copy if newer
Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"



2: When Using applicationinstead of siteoforiginin the source path, you have to possible ways:

2:在源路径中使用应用程序而不是siteoforigin时,您必须采用可能的方法:

a) Image will be in a SubFolder called "Resources" and .exe file will be small

a) 图像将位于名为“Resources”的子文件夹中,.exe 文件将很小

Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Content
Copy to output directory = Copy if newer

b) Image will be included in the .exe and no Subfolder with imagefile will exist

b) 图像将包含在 .exe 中,并且不存在带有图像文件的子文件夹

Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Resource
Copy to output directory = Copy if newer

回答by Darien Pardinas

In my case I had the images in a separate project named Commonand the images were under a folder named Resourcesin this project. In my other project, I added a reference to Commonand set the source of the images like this:

在我的情况下,我将图像放在一个名为的单独项目中Common,并且图像Resources位于该项目中命名的文件夹下。在我的另一个项目中,我添加了一个对Common图像的引用并像这样设置图像的来源:

<Image Source="/Common;component/Resources/anImage.png"/>

The images have the Build Actionset to Resourceand Copy to Output Directoryto Do not copy. However, for some strange reason it wasn't working until I deleted every assembly file in my solution and made a Clean Solutionand Build Solution. Not sure why, but it all started working at runtime once I rebuilt everything. I still can't figure out why it was working at Design Time though.

该图像具有Build Action设置为ResourceCopy to Output DirectoryDo not copy。但是,由于某种奇怪的原因,直到我删除了解决方案中的每个程序集文件并创建了一个Clean SolutionBuild Solution. 不知道为什么,但是一旦我重建了所有东西,它就在运行时开始工作。我仍然无法弄清楚为什么它在设计时工作。

回答by Amir Hossein Jamsidi

You should add any thing inside Solution Explorerof Visual Studio. Instead of just copying the image to folder in Windows Explorer, press Right Click on any folder in Solution Explorer go to Add > Existing Item...and select the path to your resource to be added.

您应该在Visual Studio 的解决方案资源管理器中添加任何内容。不只是将图像复制到 Windows 资源管理器中的文件夹,而是在解决方案资源管理器中的任何文件夹上按右键单击,转到添加 > 现有项目...并选择要添加的资源的路径。

回答by Deepak

Go to your image present in resources folder, right click on the image, go to properties. Click on BuildAction property and set it to Resource from none. That'll work.

转到资源文件夹中的图像,右键单击图像,转到属性。单击 BuildAction 属性并将其从无设置为 Resource。那行得通。

回答by Felix D.

Assumeing that you have

假设你有

  • set your Build Actionto Resource

  • set that path correctly using URI-PACK-FORMAT

  • 设置你Build ActionResource

  • 使用正确设置该路径 URI-PACK-FORMAT

In my case it was still not showing.

就我而言,它仍然没有显示。

Clean & RebuildNOT just Buildfixed it for me !

Clean & Rebuild不仅仅是 Build为我修复了它!

回答by Mari

I defined my image as next:

我将我的图像定义为下一个:

<Image Source="/Resources/Images/icon.png"/>

The image is displayed in Visual Studiodesigner but no image is displayed when I launched the app! It made me nuts! I tried all Build Actions with clean/build, no luck.

图像显示在Visual Studio设计器中,但当我启动应用程序时没有显示图像!它让我发疯了!我用 clean/build尝试了所有Build Action,但没有运气。

In my case the problem is caused by the fact that the control (which uses Image) and the app itself are in different projects. Resources/Imagesfolder is in the Controlsproject. As result the app attempted to find icon.png in its own Debug folder, but actually it is in Controls' Debug folder.

在我的情况下,问题是由控件(使用Image)和应用程序本身在不同的项目中引起的。Resources/Images文件夹位于Controls项目中。结果,应用程序试图在它自己的 Debug 文件夹中找到 icon.png,但实际上它在Controls的 Debug 文件夹中。

So two solutions work for me:

所以有两个解决方案对我有用:

1) put Resources/Imagesin the app's project (not so good when there are several projects which use controls from Controlsproject, but it works)

1) 将资源/图像放在应用程序的项目中(当有几个项目使用Controls项目中的控件时不太好,但它可以工作)

2) specify the name of Controls project explicitly inside Image:

2) 在Image 中明确指定 Controls 项目的名称:

<Image Source="/Controls;component/Resources/Images/icon.png"/>

回答by Neo

Source="file:///D:/100x100.jpg"/>works for me.

Source="file:///D:/100x100.jpg"/>对我有用

回答by ???? ?????

Make a new folder and put your pictures in the new folder and write this in XAML

创建一个新文件夹并将您的图片放在新文件夹中并在 XAML 中写入

<Image Source="/newfolder/icon.png"/>