Can I update a live tile in Mango using local data?

天大地大妈咪最大 提交于 2019-12-19 09:11:14

问题


I have a Mango WP7.5 app that uses a local SqlCe database. I would like to add a LiveTile update that shows info taken from the local DB based on current day and month.

All the samples that I've found update the background by downloading remote images from servers but I would simply need to make a local database query and show a string in my tile.

Can I do it? How?


回答1:


Yes, you can. You have to

  1. generate an image containing your textual information
  2. save this image to isolated storage and
  3. access it via isostore URI.

Here is code showing how to do this (it updates the Application Tile):

// set properties of the Application Tile
private void button1_Click(object sender, RoutedEventArgs e)
{
    // Application Tile is always the first Tile, even if it is not pinned to Start
    ShellTile TileToFind = ShellTile.ActiveTiles.First();

    // Application Tile should always be found
    if (TileToFind != null)
    {
        // create bitmap to write text to
        WriteableBitmap wbmp = new WriteableBitmap(173, 173);
        TextBlock text = new TextBlock() { FontSize = (double)Resources["PhoneFontSizeExtraLarge"], Foreground = new SolidColorBrush(Colors.White) };
        // your text from database goes here:
        text.Text = "Hello\nWorld";
        wbmp.Render(text, new TranslateTransform() { Y = 20 });
        wbmp.Invalidate();

        // save image to isolated storage
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            // use of "/Shared/ShellContent/" folder is mandatory!
            using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/MyImage.jpg", System.IO.FileMode.Create, isf))
            {
                wbmp.SaveJpeg(imageStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
            }
        }

        StandardTileData NewTileData = new StandardTileData
        {
            Title = "Title",
            // reference saved image via isostore URI
            BackgroundImage = new Uri("isostore:/Shared/ShellContent/MyImage.jpg", UriKind.Absolute),
        };

        // update the Application Tile
        TileToFind.Update(NewTileData);
    }
}


来源:https://stackoverflow.com/questions/8027812/can-i-update-a-live-tile-in-mango-using-local-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!