背景任务;XamlRenderingBackgroundTask;渲染目标位图

本文关键字:目标 位图 XamlRenderingBackgroundTask 任务 背景 | 更新日期: 2023-09-27 18:29:38

im正在开发Live Tile应用程序,以在Live Tile上显示当前网络状态。

我已经有了一个正在工作的XamlRenderingBackgroundTask,我可以从XML文件加载自定义平铺模板,并用RenderTargetBitmap将其渲染到PNG文件。

但如果我启动应用程序并注册后台任务,我必须等到后台任务开始看到更新的Live Tile。

据我所知,不可能从主应用程序中使用此代码。RendertargetBitmap需要XAML树中的元素或XamlRenderingBackgroundTask。

那么,在注册任务后,我如何直接更新Live Tile?

        var small_folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
        var small_file = await small_folder.GetFileAsync("WWAN_Tile_small.xml");
        string small_szCustomTileXML = await Windows.Storage.FileIO.ReadTextAsync(small_file);
        Border small_tile = XamlReader.Load(small_szCustomTileXML) as Border;
        if (null != small_tile)
        {
                Grid grid = small_tile.Child as Grid;
                Image logo = grid.FindName("img_Center") as Image;
                var logo_img = new BitmapImage() { CreateOptions = BitmapCreateOptions.None };
                logo_img.UriSource = new Uri("ms-appx:///Assets/Signal/WWAN/big/3.png");
                logo.Source = logo_img;
                RenderTargetBitmap rtb = new RenderTargetBitmap();
                await rtb.RenderAsync(small_tile, 71, 71);
                IBuffer pixels = await rtb.GetPixelsAsync();
                DataReader dReader = Windows.Storage.Streams.DataReader.FromBuffer(pixels);
                byte[] data = new byte[pixels.Length];
                dReader.ReadBytes(data);
                var outputFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("Square71x71Logo.scale-100.png", Windows.Storage.CreationCollisionOption.ReplaceExisting);
                var outputStream = await outputFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
                BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outputStream);
                enc.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 96, 96, data);
                await enc.FlushAsync();
        }

背景任务;XamlRenderingBackgroundTask;渲染目标位图

我认为你不能在他注册后立即强制执行BGTask;您可以创建一个新的外部类来更新tile,并在注册后立即在BGTask和应用程序中使用它。效果应该是一样的。