c# - 如何在 UWP 后台任务中使用 XAML UI 元素?

标签 c# xaml uwp background-task

问题: 我正在尝试使用其他一些 UI 元素(主要是文本 block )创建一个网格,然后使用 RenderTargetBitmap 渲染网格并最终将其保存到图像文件,在后台任务

方法代码如下:

  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();
                }
}

但是,当我从后台任务的 Run() 方法中调用此方法时,出现以下错误: enter image description here

我用谷歌搜索了一下这个异常,发现为了从非 UI 线程更新 UI 元素,我需要使用这个:

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

但即使这样也给了我一个异常(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

据我所知,在将 UI 元素添加到其中之前需要一个 View ,但我必须渲染网格并将其保存到后台任务中的图像。 以前,我在 Windows Phone 8.1 中毫无问题地实现了这一点。

是否无法在后台任务或非 UI 任务中使用 UI 元素? 如果是,我该怎么做?

最佳答案

我相信你忘了实现 XamlRenderingBackgroundTask .

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

关于c# - 如何在 UWP 后台任务中使用 XAML UI 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45656868/

相关文章:

c# - 旋转 3d 模型 XAML c#

xaml - Windows 手机 : no trigger in xaml?

c# - 如何在 c#/xaml metro 应用程序中标记时间线时将书签放在 slider 上

c# - UWP更改AppBarButton的样式打破了动态溢出的自动样式

javascript - 如何从 asp :DropDownList OnSelectedIndexChanged? 弹出 Alert()

c# - 为什么 TextChanged 事件在此 Xamarin.Android 代码中不起作用?

c# - 为什么 Winforms.Button.Text 适用于 DataBinding 而不适用于 ImageKey?

c# - ASP .NET 中的首选验证

uwp - 使用 UWP 主从更改详细信息项目

xaml - 分配附加属性值