首页 > 解决方案 > 图像未显示在窗口中,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 打开时,我只能看到用户配置文件标签,没有图像。

标签: c#wpfxaml

解决方案


推荐阅读