WPF 关于显示闪屏图像的问题

发布于 2024-11-14 19:57:14 字数 1450 浏览 2 评论 0原文

我的项目中有一个图像。

在解决方案资源管理器中,我创建了一个名为“资源”的文件夹,并向该文件夹添加了一个图像。

我想将图像设置为该文件夹中的文件。这是我的 C# 代码,

BitmapImage splashScreenImageSource = new BitmapImage();
splashScreenImageSource.BeginInit();
splashScreenImageSource.UriSource = new Uri("world_splash.jpg", UriKind.Relative);
splashScreenImageSource.EndInit();            
splashScreenImage.Source = splashScreenImageSource;

当我运行时,没有任何反应。

这是我的 XAML 代码,

<Window x:Class="Bail.SplashScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="256" Width="456" Background="#00005555" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterScreen" >
    <Grid Width="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>        
        <Image x:Name="splashScreenImage" Stretch="Fill" Grid.Row="0" />
    </Grid>
</Window>

感谢您的帮助。 如果您需要查看的话,这里是 App.xaml

<Application x:Class="Bail.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="SplashScreen.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

I have an image in my project.

In Solution Explorer, I've created a folder called "Resources" and I've added a single image in to that folder.

I want to set the image to the file in that folder. Here's my C# code

BitmapImage splashScreenImageSource = new BitmapImage();
splashScreenImageSource.BeginInit();
splashScreenImageSource.UriSource = new Uri("world_splash.jpg", UriKind.Relative);
splashScreenImageSource.EndInit();            
splashScreenImage.Source = splashScreenImageSource;

When I run, nothing happens.

Here's my XAML code

<Window x:Class="Bail.SplashScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="256" Width="456" Background="#00005555" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterScreen" >
    <Grid Width="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>        
        <Image x:Name="splashScreenImage" Stretch="Fill" Grid.Row="0" />
    </Grid>
</Window>

Thank you for all your help.
Here is App.xaml if you needed to see

<Application x:Class="Bail.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="SplashScreen.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-11-21 19:57:14

我认为你必须指定图像路径中的子目录。你说它在“资源”中。因此,请尝试使用:

splashScreenImageSource.UriSource = new Uri("Resources/world_splash.jpg", UriKind.Relative);

编辑:您必须将文件类型设置为资源才能正常工作。如果您想将其作为文件系统中的文件(而不是嵌入到程序集中),则必须使用包 uri,例如:

splashScreenImageSource.UriSource = new Uri("pack://siteoforigin:,,,/Resources/world_splash.jpg", UriKind.Relative);

在这种情况下,请确保设置文件的“复制到输出目录”选项。

还可以考虑直接从 XAML 进行初始化。它看起来更干净并且需要更少的代码:

<Image x:Name="splashScreenImage" Stretch="Fill" Grid.Row="0" Source="Resources/world_splash.jpg" />

I think you have to specify the subdirectory in the image path. You said it's in "Resources". So try using:

splashScreenImageSource.UriSource = new Uri("Resources/world_splash.jpg", UriKind.Relative);

Edit: You have to set the file type to resource for this to work. If you want to have it as a file in the file system (instead of embedded in your assembly) you have to use a pack uri, like:

splashScreenImageSource.UriSource = new Uri("pack://siteoforigin:,,,/Resources/world_splash.jpg", UriKind.Relative);

In this case make sure the "Copy to output directory" option of the file is set.

Also consider doing the initalization directly from XAML. It looks cleaner and requires less code:

<Image x:Name="splashScreenImage" Stretch="Fill" Grid.Row="0" Source="Resources/world_splash.jpg" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文