locked
Custom Live Tile from Background Task

    Question

  • I'm writing a universal app and I'd like to update the live tile in the background task I have created. I do not want to use one of the standard templates. Ideally I want to render xaml to a texture and use this as the tile template. I've seen lots of examples on doing this while the app is running but when in a background task I don't think I can use the same code.

    private async Task CreateLiveTile()
            {
                StorageFile imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("wideTile.png", CreationCollisionOption.ReplaceExisting);
                using (var stream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var bmp = new RenderTargetBitmap();
    #if WINDOWS_APP
                    wideTile.ShowSolidBackground = true;
    #else
                    wideTile.ShowSolidBackground = false;
    #endif
                    await bmp.RenderAsync(wideTile);
                    var pixels = await bmp.GetPixelsAsync();
                    var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight,
                        (uint)bmp.PixelWidth, (uint)bmp.PixelHeight, logicalDpi, logicalDpi,
                        pixels.ToArray());
                    await encoder.FlushAsync();
    
                    SetLiveTileImage("wideTile.png", "mediumTile.png");
                }
            }

    The problems I've got with this code are
    1. wideTile is defined in the XAML of a page in my app just off the drawable area. The XAML and my app is not loaded in the background thread
    2. pixels.ToArray doesnt seem to exist in the background task project

    Can anyone advise on the best way to achieve this?

    Monday, June 9, 2014 12:55 PM

Answers

  • Pushing tile updates from a server is generally better than running a background task, but it depends on the specific scenario.

    Generating the image on the server will be similar to generating it from a background task. Neither will have access to the app's Xaml to render the image and so will need to use other imaging API such as Direct2D.

    --Rob


    Monday, June 9, 2014 4:47 PM
    Owner
  • you can render images in background task with Direct2D. But still I would recommend to do this server side: http://code.msdn.microsoft.com/windowsapps/High-Performance-tile-5d4d8e02

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, June 10, 2014 12:03 PM

All replies

  • You can use only the standard templates. Custom templates are not supported.

    RenderTargetBitmap requires the target be in the active visual tree, so it cannot be used from a background task. You will need to render your image some other way. You cannot generate it based off of Xaml.

    Monday, June 9, 2014 1:33 PM
    Owner
  • Shame. So if I want to do this, I would have to send the image from my server as part of a raw notification or is that not possible either?
    Monday, June 9, 2014 1:54 PM
  • Pushing tile updates from a server is generally better than running a background task, but it depends on the specific scenario.

    Generating the image on the server will be similar to generating it from a background task. Neither will have access to the app's Xaml to render the image and so will need to use other imaging API such as Direct2D.

    --Rob


    Monday, June 9, 2014 4:47 PM
    Owner
  • I have two scenarios. One is push notifications which update the tile image. The other is a timer that checks the server for specific messages and updates the tile based on that. My plan with push was to push a raw notification that would construct the tile.

    my server is php so I can create the image with gd and download it in the background task if it has changed. It's just more load on my server I was trying to avoid the extra load on the server but if that's the only way...
    Monday, June 9, 2014 6:48 PM
  • you can render images in background task with Direct2D. But still I would recommend to do this server side: http://code.msdn.microsoft.com/windowsapps/High-Performance-tile-5d4d8e02

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, June 10, 2014 12:03 PM