Get Application Title from Windows Phone

In a Windows Phone application that is currently in development, I needed to be able to retrieve the main Application Title of the phone application. You can set the value for Deployment Title in the properties sheet of your Windows Phone Application, however getting to this value programmatically can be a little tricky.

This article assumes that you have Visual Studio 2010 and the Windows Phone tools installed along with it. The Windows Phone tools must be downloaded separately and installed after Visual Studio 2010. You may also download the free Visual Studio 2010 Express for Windows Phone developer environment.

The WMAppManifest.xml File

First off, you need to understand that when you set the Deployment Title in the Properties sheet of your Windows Phone application, this title is stored in an XML file located under the \Properties folder of your application. This XML file is named WMAppManifest.xml. A portion of this file is shown in the following listing.

<?xml version="1.0" encoding="utf-8"?>
<Deployment
  xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment"
  AppPlatformVersion="7.0">
  <App xmlns=""
       ProductID="{71d20842-9acc-4f2f-b0e0-8ef79842ea53}"
       Title="Mobile Time Track"
       RuntimeType="Silverlight"
       Version="1.0.0.0"
       Genre="apps.normal"
       Author="PDSA, Inc."
       Description="Mobile Time Track"
       Publisher="PDSA, Inc.">
 ...
 ...
  </App>
</Deployment>

Notice the “Title” attribute in the <App> element in the above XML document. This is the value that gets set when you modify the Deployment Title in the Properties window of your Phone project. The only value you can set from the Properties window is the Title. All of the other attributes you see here must be set by going into the XML file and modifying them directly.

Note that this information duplicates some of the information that you can also set from the Assembly Information… button in the Properties window. We can only speculate why Microsoft did not just use that information instead of creating another file to maintain.

Reading Attributes from WMAppManifest

Next, I searched through all the namespaces and classes within the Windows Phone DLLs and could not find a way to read the attributes within the <App> element using a Windows Phone class. Thus, I had to resort to good old fashioned XML processing. So, I created a WinPhoneCommon class and added two static read-only properties as shown in the snippet below:

public class WinPhoneCommon
{
  /// <summary>
  /// Get the Application Title
  /// from the WMAppManifest.xml file
  /// </summary>
  public static string GetApplicationTitle
  {
    get { return GetWinPhoneAttribute("Title"); }
  }

  /// <summary>
  /// Get the Application Description
  /// from the WMAppManifest.xml file
  /// </summary>
  public static string GetApplicationDescription
  {
    get { return GetWinPhoneAttribute("Description"); }
  }

  ... GetWinPhoneAttribute method here ...
}

In your Windows Phone application you can now simply call WinPhoneCommon.GetApplicationTitle or WinPhone.GetApplicationDescription to retrieve the Title or Description properties from the WMAppManifest.xml file respectively. You notice that each of these properties makes a call to the GetWinPhoneAttribute method. This method is shown in the following code snippet:

/// <summary>
/// Gets an attribute from the Windows Phone WMAppManifest.xml file
/// To use this method, add a reference to the System.Xml.Linq DLL
/// </summary>
/// <param name="attributeName">The attribute to read</param>
/// <returns>The Attribute's Value</returns>
private static string GetWinPhoneAttribute(string attributeName)
{
  string ret = string.Empty;

  try
  {
    XElement xe = XElement.Load("WMAppManifest.xml");
    var attr = (from manifest in xe.Descendants("App")
                select manifest).SingleOrDefault();
    if (attr != null)
      ret = attr.Attribute(attributeName).Value;
  }
  catch
  {
    // Ignore errors in case this method is called
    // from design time in VS.NET
  }

  return ret;
}

I love using the new LINQ to XML classes contained in the System.Xml.Linq.dll. When I did a Bing search the only samples I found for reading attribute information from WMAppManifest.xml used either an XmlReader or XmlReaderSettings objects. These are fine and work, but involve a little extra code. Instead of using these, I added a reference to the System.Xml.Linq.dll, then added two using statements to the top of the WinPhoneCommon class:

using System.Linq;
using System.Xml.Linq;

Now, with just a few lines of LINQ to XML code you can read to the App element and extract the appropriate attribute that you pass into the GetWinPhoneAttribute method.

Notice that I added a little bit of exception handling code in this method. I ignore the exception in case you call this method in the Loaded event of a user control. In design-time you cannot access the WMAppManifest file and thus an exception would be thrown.

Summary

In this article you learned how to retrieve the attributes from the WMAppManifest.xml file. I use this technique to grab information that I would otherwise have to hard-code in my application. Getting the Title or Description for your Windows Phone application is easy with just a little bit of LINQ to XML code.

NOTE: You can download the complete sample code at my website. http://www.pdsa.com/downloads. Choose Tips & Tricks, then "Get Application Title from Windows Phone" from the drop-down.

Good Luck with your Coding,
Paul Sheriff

** SPECIAL OFFER FOR MY BLOG READERS **
Visit http://www.pdsa.com/Event/Blog for a free video on Silverlight entitled Silverlight XAML for the Complete Novice - Part 1.

Past Blog Content

Blog Archive

5 Comments

  • Is there a way to write back to the manifest file? Thanks!

  • Thanks for your reply Paul. My goal was to update the IconPath element with new app icon name (say my app has this icon when in this state, and that other icon when in that other state). So basically, is there a way to set the app icon at runtime, or can it only be set through the deployment options/WPManifest.xml?

    FYI, I thought I had managed to write with the following but I don't know where because the change either isn't committed to the file, or it's saved in different file somewhere else:
    using (XmlWriter writer = XmlWriter.Create(new System.Text.StringBuilder("WMAppManifest.xml")))
    {
    xe.Save(writer);
    }

  • Hi Oliver,

    Yes, the Create method of the XmlWriter will not error, but nothing is written to disk. The best way to do this is add both icons to your project, then write which one is the current one to isolated storage. Read this value in when you start the application.

    Hope this helps,
    Paul

  • Hi Paul

    I'm still confused about which property to set with the value I read when the app starts.

    Thanks!

    Olivier.

  • Olivier,

    So you are talking about the main application icon I assume. I will see if I can write a blog post on this. No guarantees when I will get to this though. :)

    Paul

Comments have been disabled for this content.