xaml 图像在运行时不显示,但在设计期间显示(VB、wpf) - 为什么? [英] xaml image not displayed during runtime but displaying during design (VB, wpf) - why?

查看:31
本文介绍了xaml 图像在运行时不显示,但在设计期间显示(VB、wpf) - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 WPF 并创建了一个 WPF 测试项目.我已经在每个文件夹中放置了一个 monkey01.jpg 图像,从项目的根目录开始,在层次结构中向上移动到 debug 文件夹.在 MainWindow 上,我创建了一个 Image 并定义了 source 属性 以指向 monkey01.jpg 和它在设计视图中正确显示.但是当我运行它时,图像没有出现.

I'm trying to understand WPF and have created a WPF test project. I've put a monkey01.jpg image in every folder starting from the project's root, moving up in hierarchy, to the debug folder. On the MainWindow I've created an Image and defined the source property to point to the monkey01.jpg and it displays correctly in design view. But when I run it, the image doesn't appear.

  • 如果我将图像文件作为资源添加到项目,它将在预期的运行时显示.(但这不是我在这里测试的)
  • 如果在 xaml 文件中,我将 绝对路径 写入 Source="C:\Users\User\Desktop\visual_studio_projects\WpfApplication1\monkey01.jpg 也会显示出来.
  • 但如果我使用相对路径(如下例所示),则不会显示图像.就好像在执行时,程序不会在项目文件夹及其子文件夹中运行;相反,它运行在无法访问图像的其他位置.为什么会有这种行为?我错过了什么?
  • If I add the image file to the project as a resource, it will be displayed during runtime which is expected. (But this is not what I'm testing here)
  • If in the xaml file I write the absolute path as in Source="C:\Users\User\Desktop\visual_studio_projects\WpfApplication1\monkey01.jpg it will be displayed too.
  • But if I use a relative path (like in the example below), the image won't be displayed. It's as if upon execution, the program doesn't run inside the project folder and its subfolders; Instead it runs in some other location that has no access the image. Why this behaviour? What am I missing?

这是MainWindow.xaml 代码:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image x:Name="Thumbnailtest" Source="monkey01.jpg" />
    </Grid>
</Window>

谢谢!

推荐答案

当我将图像设置为 Resource 并且不复制时在这里工作正常,请注意我将您的猴子图像更改为 .png 并创建了一个文件夹它(数据/图像).

Works fine here when I've set the image to Resource and don't copy, note I changed your monkey image to .png and I created a folder for it(Data/Images).

<Window x:Class="MasoneryLibrary.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MasoneryLibrary;assembly=MasoneryLibrary"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid>
        <Image Source="Data/Images/monkey01.png" HorizontalAlignment="left" VerticalAlignment="Top" Width="290" Height="456"  Margin="285,0,-58,-136" Stretch="None" StretchDirection="DownOnly"/>
    </Grid>
</Grid>

嗯……这是图书馆吗?您应该使用URI 包为了安全.

Hmmmm.. is this a library? You should use URI packs to be safe.

使用 URI 包:

<Image Source="pack://application:,,,/MasoneryLibrary;component/Data/Images/monkey01.png" HorizontalAlignment="left" VerticalAlignment="Top" Width="290" Height="456"  Margin="285,0,-58,-136" Stretch="None" StretchDirection="DownOnly"/>

希望有帮助!

干杯,

斯蒂安

这篇关于xaml 图像在运行时不显示,但在设计期间显示(VB、wpf) - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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