簡體   English   中英

WPF 關於啟動畫面顯示圖像的問題

[英]WPF Question about displaying the image for Splash Screen

我的項目中有一張圖片。

在解決方案資源管理器中,我創建了一個名為“資源”的文件夾,並在該文件夾中添加了一個圖像。

我想將圖像設置為該文件夾中的文件。 這是我的 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>

我認為您必須在圖像路徑中指定子目錄。 你說它在“資源”中。 所以嘗試使用:

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

編輯:您必須將文件類型設置為資源才能使用。 如果要將其作為文件系統中的文件(而不是嵌入到程序集中),則必須使用 pack uri,例如:

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

在這種情況下,請確保設置了文件的“復制到 output 目錄”選項。

還可以考慮直接從 XAML 進行初始化。 它看起來更干凈,需要更少的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM