wpf 图像显示在 Visual Studio 设计器中,但不在运行时显示

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

Image shows up in Visual Studio designer but not during run-time

wpfvisual-studio-2010imagexaml

提问by d.moncada

I'm currently working w/ VS studio 2010, .NET 4.0. Here is my scenario:

我目前正在使用 VS studio 2010、.NET 4.0。这是我的场景:

I currently have a class library project called "Images" that contains a sub folder called "Icons". All my icons currently live in this project and are accessed by multiple projects via siteoforigin. Everything shows up fine while in the designer, but during run time the images do not display.

我目前有一个名为“Images”的类库项目,其中包含一个名为“Icons”的子文件夹。我的所有图标目前都存在于这个项目中,并且可以通过 siteoforigin 被多个项目访问。在设计器中一切正常,但在运行时图像不显示。

Here is my folder structure:

这是我的文件夹结构:

  • Root Folder
    • Images
      • Icons
    • Project 1
    • Project 2
    • Project 3
  • 根文件夹
    • 图片
      • 图标
    • 项目一
    • 项目二
    • 项目3

All projects are referencing the Images class lib. proj, and all icons inside the "icons" sub-folder within the "Images" project is set to: "Build Action = Resource".

所有项目都引用图像类库。proj,并且“Images”项目中“icons”子文件夹中的所有图标都设置为:“Build Action = Resource”。

The xaml looks like this (which is perfectly visible during design time):

xaml 看起来像这样(在设计时完全可见):

     <Button Name="Button1" Click="Button1_Click">
        <Button.Content>
            <Image Source="pack://siteoforigin:,,,/Data.Images;component/Icons/Button1.png" />
        </Button.Content>
    </Button>

采纳答案by H.B.

siteoforiginis for files copied to the executable directory or subdirectories, for resources you should use applicationas authority as far as i know.

siteoforigin用于复制到可执行目录或子目录的文件,对于资源application,据我所知,您应该将其用作权限。

SiteOfOrigin:

原产地:

Path: The name of the site of origin file, including its path relative to the location from which the executable assembly was launched.

路径:源文件站点的名称,包括其相对于启动可执行程序集的位置的路径。

回答by Markb

This was driving me crazy. I tried lots of different things based on various articles but nothing worked. My xaml window and image are in a separate DLL from the executable assembly. Not sure if that was part of my problem.

这让我发疯。我根据各种文章尝试了很多不同的东西,但没有任何效果。我的 xaml 窗口和图像与可执行程序集位于单独的 DLL 中。不确定这是否是我问题的一部分。

In the end I had success with:

最后我成功了:

  1. Make sure the image properties in the project has "Build Action" = "Resource", NOT "Embedded Resource"
  2. 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.
  1. 确保项目中的图像属性具有“Build Action”=“Resource”,而不是“Embedded Resource”
  2. 在 xaml 中,选择图像标记后,使用属性窗口选择源下拉列表,因为现在图像出现在下拉列表中!这让 Visual Studio 为我格式化字符串。

The string visual studio formatted for my image was:

为我的图像格式化的字符串视觉工作室是:

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

where "Paperless.Common" was the name of my assembly, and "Checkbook.png" was my image that resided in a "Resources" folder.

其中“Paperless.Common”是我的程序集的名称,“Checkbook.png”是我位于“Resources”文件夹中的图像。

回答by Amine Da.

Maybe the Image does not have the Build Action set to Resource. If it's anything else, including Embedded Resource, then it will not display properly at runtime.

也许图像没有将构建操作设置为资源。如果它是其他任何东西,包括嵌入式资源,那么它在运行时将无法正确显示。

回答by Mohamed Hussein

Well, I waisted more than 3 hours of my time on this problem. Your answers helped me to partially solve the problem and that's what urged me to add to this useful discussion.

好吧,我在这个问题上浪费了超过 3 个小时的时间。您的回答帮助我部分解决了问题,这也是促使我加入这个有用讨论的原因。

I got my problem solved as:

我的问题解决了:

  1. select all images in your solution explorer resources.
  2. Right click and go to properties.
  3. Build action to "Embedded Resource"
  4. Copy to Output to "Copy Always"
  5. save and build solution.
  6. add image and take care or the ZIndex value.
  7. After all of that I found my self when I use the image multiple times or try to change to another image on the same image control to change the path for the image either on XAML or in the image control properties to be:

    source="pack:://siteoforigin:,,,/Resources/image.png"
    
  1. 选择解决方案资源管理器中的所有图像。
  2. 右键单击并转到属性。
  3. 为“嵌入式资源”构建操作
  4. 复制到输出到“始终复制”
  5. 保存并构建解决方案。
  6. 添加图像并注意或 ZIndex 值。
  7. 毕竟,当我多次使用图像或尝试更改为同一图像控件上的另一个图像以将 XAML 或图像控件属性中的图像路径更改为:

    source="pack:://siteoforigin:,,,/Resources/image.png"
    

回答by Dirk Bester

I came here to get my design time image data to show up (inverse problem).

我来这里是为了让我的设计时图像数据显示出来(反问题)。

ImagePath="pack://application:,,,/MyApplicationName;component/Images/Icons/Button1.png"is what did the trick, in addition to Build Actionset to Resourceand Copy to Output Directoryset to Copy if newer.

ImagePath="pack://application:,,,/MyApplicationName;component/Images/Icons/Button1.png"除了Build Actionset toResourceCopy to Output Directoryset to之外,还有什么诀窍Copy if newer

回答by Nehorai

The following worked for me:

以下对我有用:

? Build Action = Resource

? 构建操作 = 资源

? Copy to Output Directory = Do not copy

? 复制到输出目录 = 不复制

xaml:

xml:

<Image Source=“Rsources/myPic.png”/>

Where “myPic.png” is the image inside “Resources” folder

其中“myPic.png”是“Resources”文件夹中的图像

回答by Santhakumar

I had same problem.
Now its working...
Try this:

我有同样的问题。
现在它的工作......
试试这个:

<Button x:Name="b2" HorizontalAlignment="Left" VerticalAlignment="Top"
    Width="26" Height="29"> 
    <Button.Background> 
        <ImageBrush ImageSource=" a.png"/> 
    </Button.Background>
</Button>

回答by Chetan Verve

Please check image is included in solution or not. If image is not included then click on Show All Files from solution and then right click on image and select Include In Project option. After that it will work with relative path as well.

请检查图像是否包含在解决方案中。如果不包含图像,则单击“显示解决方案中的所有文件”,然后右键单击图像并选择“包含在项目中”选项。之后它也可以使用相对路径。

Ex:

前任:

" Source="/Images/Logo/close.png" "

" 来源="/图片/标志/close.png" "

回答by Joish

If you are using images from DLL, make sure that dll project has default resources file setup in Project's properties. and change its access modifier to public. Also, set images build action to "Resource"

如果您使用来自 DLL 的图像,请确保 dll 项目在项目的属性中设置了默认资源文件。并将其访问修饰符更改为 public。此外,将图像构建操作设置为“资源”

回答by trueCamelType

To anyone who may not try this first, if you attempt some of the fixes mentioned here, and nothing is working, try cleaning and rebuilding your solution. I just forgot to add the images to the project (Create images folder, right click folder -> add existing -> click on image).

对于可能不先尝试此操作的任何人,如果您尝试了此处提到的一些修复程序,但没有任何效果,请尝试清理并重建您的解决方案。我只是忘了将图像添加到项目中(创建图像文件夹,右键单击文件夹 -> 添加现有 -> 单击图像)。

That didn't fix it until I did a clean and rebuild.

直到我进行了清理和重建后才解决它。