【问题标题】:WPF Question about displaying the image for Splash ScreenWPF 关于为启动画面显示图像的问题
【发布时间】:2011-06-09 13:25:37
【问题描述】:

我的项目中有一张图片。

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

我想将图像设置为该文件夹中的文件。这是我的 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>

【问题讨论】:

    标签: c# wpf visual-studio-2010 c#-4.0 splash-screen


    【解决方案1】:

    我认为您必须在图像路径中指定子目录。你说它在“资源”中。所以尝试使用:

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

    编辑:您必须将文件类型设置为资源才能使用。如果您想将其作为文件系统中的文件(而不是嵌入到程序集中),您必须使用 pack 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" />
    

    【讨论】:

    • 我想在我的应用程序中包含这些资源。当我分发它时,我不能确定它是否会在文件系统上可用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2013-10-17
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多