2

I'm searching for a good and fast way to load images as background brushes to Windows 10 Mobile UWP XAML pages (Grid background).

Currently I do this like the following:

  1. Define ImageBrush in Page.Resources section
  2. Use defined ImageBrush and set Background to this resource

First step:

<Page.Resources>
   <ImageBrush x:Key="background"   ImageSource="msappx:///Assets/Background/bg_login.jpg" 
               Stretch="UniformToFill" Opacity="0.25"/>
</Page.Resources>

Second step:

<Grid Background="{StaticResource background}">
    <Grid.CacheMode>
       <BitmapCache />
    </Grid.CacheMode>
    <!-- Some other UI components -->
</Grid>

Unfortunately when the page comes up for the first time, the background brush needs at least 1.5 seconds to be displayed. So in the meantime, there is just a white background, which is not what I want to achieve. My background image "bg_login.jpg" is already compressed to round about 40kb.

Is there a faster way to display the image as a grid background? Or do I maybe have the opportunity to load the image while the splashscreen is showing?

Any help would be greatly appreciated. Thanks in advance.

user1011394
  • 1,656
  • 6
  • 28
  • 41
  • 1
    I don't have access to UWP resources at the moment, but have you attempted putting that `ImageBrush` in your App.xaml resources? In WPF it's Freezable, but it seems that isn't a thing in UWP. The idea is to at least have it loaded and ready as your app comes up. You'll have to give it a more meaningful name than "background" to distinguish it from other background brushes. – Berin Loritsch Jun 15 '16 at 19:35
  • Hi Berin, mhh that's a great idea. I'll give it a try. Of course, in app.xaml resources, the name has to be a bit more unique ;). Thanks for your reply. – user1011394 Jun 15 '16 at 19:39
  • Unfortunately that doesn't work. The same behavior. You've still about 1-2 seconds first time loading time to bring up the background. Also my other approach failed...If I create the resource in a static way and reference to the resource directly at the "OnNavigated.." method, to load it, right before rendering, doesn't work well. – user1011394 Jun 15 '16 at 19:56
  • I'm sorry. Loosing the `Freezable` base class in UWP kind of limits what you can do since it has to be created in the UI thread now. So a `BitmapImage` is an `ImageSource` Under the covers your URI invokes the `BitmapImage(uri)` constructor. I wish there was a faster way of doing this, but you might be able to load the BitmapImage asynchronously: https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.bitmapsource.setsourceasync.aspx – Berin Loritsch Jun 15 '16 at 20:15

0 Answers0