UWP Windows 10 project.

It would be a seemingly simple task: to display a page with a logo and a rotating load indicator.

private async void Page_Loaded(object sender, RoutedEventArgs e) { var coreDispatcher = Window.Current.Dispatcher; await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (string.IsNullOrEmpty(SettingsManager.login) || string.IsNullOrEmpty(SettingsManager.password)) { invertUI(); } else { Signals.invokeAuthOK(); } }); } 

The ring is spinning, the logo does not appear.

 <Image Name="Logo" Source="/Assets/SplashScreen.png" Width="310" Height="150" Margin="0,0,0,20"/> <StackPanel Name="LoadBox" Width="310" VerticalAlignment="Center" HorizontalAlignment="Center"> <ProgressRing Foreground="White" IsActive="True"/> </StackPanel> 

Tell me how to execute the code after all the elements on the form are rendered?

  • Nothing is clear. And here “display the logo” to “how to execute the code after all the elements on the form are rendered”? And where does the content of your Page_Loaded to both of these questions? - VladD
  • @VladD I need to execute some code after the page is displayed. In the markup of the page there is an image and progressing, so the page does not have time to draw the image before the start of the code, after it is executed, it switches to another page. - SYL

1 answer 1

If I understand you correctly, the application moves to another page before, that it manages to render the image. Try instead of PageLoaded the page to subscribe to the ImageOpened event at the Image control.

Also, the cause of the problem may be more prosaic and consist in an unreasonably large resolution of the image. Additionally, if the presence of an alpha channel is not critical, try using an image in jpeg format.

And finally, you have the opportunity to set the resolution to which the image will be decoded (by default, for example, a FullHD image will be decoded into its native resolution, 1920x1080, even if the final result takes much less pixels on the screen). Instead of controlling Image, you will have, for example, Rectangle:

 <Rectangle Name="Logo" Width="310" Height="150" Margin="0,0,0,20"> <Rectangle.Fill> <ImageBrush> <ImageBrush.ImageSource> <BitmapImage UriSource="Assets/SplashScreen.png" DecodePixelWidth="310" DecodePixelHeight="150" DecodePixelType="Logical"/> </ImageBrush.ImageSource> </ImageBrush> </Rectangle.Fill> </Rectangle> 

Note that the BitmapImage DecodePixelType property is set to Logical: the system will determine itself what the resolution should be in physical pixels depending on the ppi and screen size of the device on which the application is running.

Both ImageBrush and BitmapImage can subscribe to the ImageOpened event, so the methods described above can be easily combined.