c# - 图像未显示在窗口中,Image.Source 似乎是有效的
问题描述
我正在开发一个 WPF 项目。我需要在新窗口打开时显示图像,但我似乎没有让它工作。我觉得我什么都试过了。
<Window x:Class="iTool.ProfileWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:iTool"
mc:Ignorable="d"
Title="iTool: User Profile" Height="800" Width="1200" ResizeMode="NoResize">
<Grid>
<StackPanel>
<Label Content="User Profile" FontSize="30" HorizontalContentAlignment="Center"/>
<StackPanel Orientation="Horizontal">
<StackPanel Width="300" Height="700">
<Image x:Name="imgUserProfile" Height="200" Width="200" Margin="50"/>
<TextBlock x:Name="txbFirstName"/>
<TextBlock x:Name="txbLastName"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</Window>
public partial class ProfileWindow : Window
{
public ProfileWindow()
{
InitializeComponent();
imgUserProfile.Source = new BitmapImage(new Uri(MainWindow.activeUserImage, UriKind.RelativeOrAbsolute));
}
}
在 Debug 手表中:activeUserImage = "images/samson_profile.jpg"
imgUserProfile.Source = {pack://application:,,,/iTool;component/images/samson_profile.jpg}
图像存在于“images”文件夹中,但不会作为现有文件添加到项目中。
当 ProfileWindow 打开时,我只能看到用户配置文件标签,没有图像。
解决方案
推荐阅读
- python - 如何将文本文件(或字符串到 QML 中的 textArea)从 python 发送到 QML 应用程序?
- spring - 如何在百里香中将一个html页面包含到另一个页面?
- typescript - TypeScript、Vue 和实验性装饰器
- powerbuilder - Powerbuilder - 如果值为 null,则 db 表上的帐户 ID 将更改为 0
- java - RxJava 2 Disposable 活动销毁
- java - 如何在线性时间内找到 2-sum?
- android - android.hardware.camera2 缩放实现
- python - 填写一列日期值,直到达到另一个日期值,然后继续填充新达到的值
- python - 无法导入不同 Python 版本的库
- javascript - 在javascript中隐藏另一个“div”后自动显示一个“div”