Search code examples
c#xamluwpbackground-task

How to use XAML UI element in UWP Background task?


Problem : I am trying to create a grid with some other UI elements (Textblocks mostly) and then use RenderTargetBitmap to Render the Grid and Finally Save it to an image file, on a Background Task

Here is the code for the method :

  private async Task<bool> RenderGridAsync()
  {
            Grid mainGrid = new Grid();
            TextBlock tText = new TextBlock()
            {
                HorizontalAlignment = HorizontalAlignment.Left,
                TextWrapping = TextWrapping.Wrap,
                Text = "Some Example Text",
                VerticalAlignment = VerticalAlignment.Top,
                FontSize = 72,
                FontWeight = FontWeights.SemiLight
            };
            mainGrid.Children.add(tText);
            RenderTargetBitmap rtb = new RenderTargetBitmap();
                await rtb.RenderAsync(mainGrid);

                var pixelBuffer = await rtb.GetPixelsAsync();
                var pixels = pixelBuffer.ToArray();
                var displayInformation = DisplayInformation.GetForCurrentView();
                var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("CurrentImage" + ".png", CreationCollisionOption.ReplaceExisting);
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Premultiplied,
                                         (uint)rtb.PixelWidth,
                                         (uint)rtb.PixelHeight,
                                         displayInformation.RawDpiX,
                                         displayInformation.RawDpiY,
                                         pixels);
                    await encoder.FlushAsync();
                }
}

But when I am calling this method from the Run() method of my background task I am getting the following error : enter image description here

I googled the exception a bit and found that in order to update UI elements from non UI thread I need to use this :

 await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher
                .RunAsync(CoreDispatcherPriority.Normal, async () => {
                await RenderGridAsync();
            });

but even this is giving me an exception :

Exception thrown: 'System.Runtime.InteropServices.COMException' in BackgroundTask.winmd
WinRT information: Could not create a new view because the main window has not yet been created

From what I get from this is that a view is expected before the UI elements can be added to it,but I have to render the grid and save it to an image in the Background task. Previously, I achieved this in Windows Phone 8.1 without any problem..

Is it not possible to work with UI elements on a background task or a Non UI task? If yes, how can I do this?


Solution

  • I believe you forgot to implement XamlRenderingBackgroundTask.

    Provides the ability to create a bitmap from a XAML tree in a background task.