Windows 8 Tile Live Notification using C++

In my blog entry: https://aka.ms/tilenotification I wrote a super simple code to demonstrate that C++ isn’t all that difficult to use with Visual Studio.  But it was a little light on details and this article is written to correct that problem.

Let’s be clear, I think that C++ isn’t the ultimate software language.  It is something that society is stuck with.  Could be worse, COBOL could have won the contest.

XAML and C++

First of all, C++ with XAML, in the XAML, you use the XAML editor just like you would with C# or Visual Basic, except that it does lag sometimes on my computer with respect to things like switching to events from properties and other latencies, hopefully these will be fixed.  The designer is somewhat slow to load on my system as well compared to the XAML loading in C#, but this might be due to debug symbols.

Naming objects in  XAML, use the x:Name format, C++ does not thrown in a default name, and you will need to.

Adding an event in C++, use the Event tool, seriously:

The Top Image is the default for the Properties showing the Properties, the bottom image is the Event version and the Click event is shown and named DemoTile1

Now let’s take a look at the C++ code

First off, many of the class type of objects are the same across all of the platforms, if you do not see a need to use C++, you should evaluate the use of C#.

This is not a religious discussion, C++/CX has it’s place, but the place is not everywhere.  Using WinRT you can project your C++/CX efforts into other languages.  You should use the most efficient language, which includes the consideration of the many personalities involved in software design.  Designers need certain kinds of UI, so Blend for Visual Studio, C# might be used by mission designers, C++ by game engine designers and Visual Basic for Database interfaces.

With that let’s tear apart the code:

XmlDocument^ tileXml = TileUpdateManager::GetTemplateContent(TileTemplateType::TileSquareText01);

XmlDocument^ the “caret” or hat allows the use of “ref new” (which we discuss later in this blog) and implements the garbage collection in the case of the XmlDocument.  This is important for the XmlDocument as it generates an XML file on the fly, and we want to get rid of it when we are done with it.

tileXML is the variable to store our XmlDocument in for later use.

TileUpdateManager::GetTemplateContent  This code utilizes the TileUpdateManager class that gets the Template Content, in the parentheses this is an enumeration that points to specific Tiles.  With the TileSquareText01 the XML generated looks like (but you see it as the nice little square with the text):

 <tile> 
     <visual> 
         <binding template="TileSquareText01"> 
             <text id="1">Text Field 1</text> 
             <text id="2">Text Field 2</text> 
             <text id="3">Text Field 3</text> 
             <text id="4">Text Field 4</text> 
         </binding> 
     </visual> 
 </tile> 

For more information: https://msdn.microsoft.com/en-us/library/windows/apps/hh761491.aspx

 

The Next 2 Lines could be five lines, and the variable that follows the hat or “caret” tileTextAttributes, is poorly chosen for an example, because it seems like a keyword, but it is actually a variable.  “tileXml” is also a variable which is assigned to the XmlDocument.

XmlNodeList^ tileTextAttributes = tileXml->GetElementsByTagName("text");

tileTextAttributes->Item(0)->InnerText = "Boo";

//These are the lines of code that could be added for additional text, in the XML shown above it is the text field 1, 2, 3 ,4.

tileTextAttributes->Item(1)->InnerText = "Line2";

tileTextAttributes->Item(2)->InnerText = "Line3";

tileTextAttributes->Item(3)->InnerText = "Line4";

 

Sending your notification

The next two lines are used to send the notification.  The ref new keyword allocates a ref class, in this case the XmlDocument in dynamic memory.  The hat on TitleNotification^ is essentially a smart pointer, and the memory that it points to is automatically destroyed when the last hat goes out of scope or set to nullptr.  With respect to hat there are reference semantics discussed elsewhere.

In the second line then updates the tile using the memory location generated by the tileNotification.

TileNotification^ tileNotification = ref new TileNotification(tileXml);

TileUpdateManager::CreateTileUpdaterForApplication()->Update(tileNotification);

 

Conclusion:

So far with C++, except for the symbols, memory allocation and a number of other mechanical things, if you are not in need of C++ you could definitely use one of the other languages.  Bear in mind that the C++/CX operates at a lower level then the other managed code processes, but the downside is that the C++/CX code might not work on ARM but works on the Intel form.