Project Description
A collection of .NET classes that enables developers to create tile notifications for Windows Store apps in a simplified way rather than using XML and DOM.

Using the TileNotificationsPack

The collection of classes that model tile notifications and the extensions for the TileUpdater make the experience of writing tile notifications for Windows Store apps much simpler. Instead of working with XML you create objects and collections in the object oriented manner you are used to.

Note: You must add a reference to assembly TileNotificationsPack.dll.

The example below shows how to create various notifications for normal size (150x150), wide (310x150) and large (310x310) tiles.

var updater = TileUpdateManager.CreateTileUpdaterForApplication();
updater.EnableNotificationQueue(true);
updater.Clear();

var imgsrc = new string[] {
  "ms-appx:///Images/image1.png",
  "ms-appx:///Images/image2.png",
  "ms-appx:///Images/image3.png",
  "ms-appx:///Images/image4.png",
  "ms-appx:///Images/image5.png",
};

for (int i = 0; i < 5; ++i)
{
  try
  {
     var tiles = new TileCollection()
     {
        new TileSquare150x150Image() {
           Binding = new TileBinding() { Branding = TileBranding.name },
           Image = new TileImage() { Src = imgsrc[i], Alt = imgsrc[i] }
        },
        new TileWide310x150ImageAndText01(){
           Image = new TileImage() { Src = imgsrc[i], Alt = imgsrc[i] },
           Text = string.Format("this is notification #{0}", i+1)
        },
        new TileSquare310x310ImageAndTextOverlay02() {
           Binding = new TileBinding() { Branding = TileBranding.none },
           Image = new TileImage() { Src = imgsrc[i], Alt = imgsrc[i] },
           Text = string.Format("this is notification #{0}", i+1),
           Text2 = string.Format("and this is a description for notification #{0}", i+1)
        }
     };

     updater.Update(tiles);
  }
  catch (Exception ex)
  {
     Debug.WriteLine(ex.Message);
  }
}

Here are screenshots of the result:
livetiles.png

Why using the pack?

Creating a tile notification for Windows Store app requires creating an XmlDocument for the tile notification and working with the DOM, which is cumbersome and error prone.

Here is an example:

const string textElementName = "text";
const string imageElementName = "image";

// Create a tile update manager for the specified syndication feed.
var updater = TileUpdateManager.CreateTileUpdaterForApplication();
updater.EnableNotificationQueue(true);
updater.Clear();

var imgsrc = new string[] {
  "ms-appx:///Images/image1.png",
  "ms-appx:///Images/image2.png",
  "ms-appx:///Images/image3.png",
  "ms-appx:///Images/image4.png",
  "ms-appx:///Images/image5.png",
};

for (int i = 0; i < 5; ++i)
{
  try
  {
     // wide 310x150
     var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150ImageAndText01);
     tileXml.GetElementsByTagName(textElementName)[0].InnerText = string.Format("this is notification #{0}", i+1);
     var image = tileXml.GetElementsByTagName(imageElementName).FirstOrDefault();
     if (image != null)
     {
        var src = tileXml.CreateAttribute("src");
        src.Value = imgsrc[i];
        image.Attributes.SetNamedItem(src);
     }

     updater.Update(new TileNotification(tileXml));
  }
  catch (Exception ex)
  {
     Debug.WriteLine(ex.Message);
  }
}

The situation becomes more cumbersome when you support multiple tile size (which is the most common scenario). In that case you need additional working with the DOM, and create an XML document with a tile that has a visual element with multiple binding children nodes.

// wide 310x150
var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150ImageAndText01);
tileXml.GetElementsByTagName(textElementName)[0].InnerText = string.Format("this is notification #{0}", i+1);
var image = tileXml.GetElementsByTagName(imageElementName).FirstOrDefault();
if (image != null)
{
  var src = tileXml.CreateAttribute("src");
  src.Value = imgsrc[i];
  image.Attributes.SetNamedItem(src);
}

// square 150x150
var squaredTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text04);
squaredTileXml.GetElementsByTagName(textElementName)[0].InnerText = string.Format("this is notification #{0}", i+1);

var binding = tileXml.ImportNode(squaredTileXml.GetElementsByTagName("binding")[0], true);
tileXml.GetElementsByTagName("visual")[0].AppendChild(binding);
         
updater.Update(new TileNotification(tileXml));

You can read more here.

Last edited Dec 21, 2013 at 10:11 PM by MariusBancila, version 6