0

im developing an Live Tile APP to show the current Network State on the Live Tile.

I already have a working XamlRenderingBackgroundTask, i can load Custom Tile Templates from XML files and i Render it with a RenderTargetBitmap to a PNG file.

But if i start the APP and Register the Background Task, i have to wait until the BackgroundTask starts to see an updated Live Tile.

As far as i know it is not possible to use this code from the Main APP. RendertargetBitmap needs an Element from the XAML Tree or a XamlRenderingBackgroundTask.

So, how can i update the Live Tile direclty after registering the Task?

        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();
        }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

I don't think you can force the BGTask to execute immediately after his registration; what you can do is create a new external class which updates the tile and use it both in the BGTask and in the app, immediately after the registration. The effect should be the same.

Federinik
  • 523
  • 4
  • 20