WPF关于为初始屏幕显示图像的问题

问题描述:

我在我的项目中有一个图像。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> 

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

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