BackgroundTask& XamlRenderingBackgroundTask& RenderTargetBitmap

时间:2014-11-08 21:54:16

标签: c# xaml windows-phone rendertargetbitmap

开发Live Tile APP以显示Live Tile上的当前网络状态。

我已经有了一个有效的XamlRenderingBackgroundTask,我可以从XML文件中加载自定义平铺模板,然后使用RenderTargetBitmap将其渲染为PNG文件。

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

据我所知,无法使用主APP中的此代码。 RendertargetBitmap需要XAML树中的元素或XamlRenderingBackgroundTask。

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

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

1 个答案:

答案 0 :(得分:0)

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