2011-06-09 68 views
2

我在我的項目中有一個圖像。WPF關於爲初始屏幕顯示圖像的問題

在解決方案資源管理器中,我創建了一個名爲「資源」的文件夾,並且已將單個圖像添加到該文件夾​​中。

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

回答

1

我認爲你必須指定在圖像路徑中的子目錄。你說它在「資源」中。因此,嘗試使用:

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" /> 
+0

我想包括我的應用程序的資源。當我分發它時,我無法確定它是否可用於文件系統。 – 2011-06-09 14:27:09