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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你必须指定图像路径中的子目录。你说它在“资源”中。因此,请尝试使用:
编辑:您必须将文件类型设置为资源才能正常工作。如果您想将其作为文件系统中的文件(而不是嵌入到程序集中),则必须使用包 uri,例如:
在这种情况下,请确保设置文件的“复制到输出目录”选项。
还可以考虑直接从 XAML 进行初始化。它看起来更干净并且需要更少的代码:
I think you have to specify the subdirectory in the image path. You said it's in "Resources". So try using:
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:
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: