Get Application Title from Windows Phone

Posted by psheriff on ASP.net Weblogs See other posts from ASP.net Weblogs or by psheriff
Published on Tue, 04 Jan 2011 20:35:00 GMT Indexed on 2011/01/04 20:54 UTC
Read the original article Hit count: 415

Filed under:
|

In a Windows Phone application that I am currently developing I needed to be able to retrieve the Application Title of the phone application. You can set the Deployment Title in the Properties 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 with Visual Studio2010. You may also download the free Visual Studio2010 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 windows of your Windows Phone application, this title actually gets stored into 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
  http://schemas.microsoft.com/windowsphone/2009/deployment">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 your 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. Why Microsoft did not just use that information, I don’t know.

Reading Attributes from WMAppManifest

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

public class WinPhoneCommon
{
  /// <summary>
  /// Returns the Application Title
  /// from the WMAppManifest.xml file
  /// </summary>
  /// <returns>The application title</returns>
  public static string GetApplicationTitle()
  {
    return GetWinPhoneAttribute("Title");
  }

  /// <summary>
  /// Returns the Application Description
  /// from the WMAppManifest.xml file
  /// </summary>
  /// <returns>The application description</returns>
  public static string GetApplicationDescription()
  {
    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 methods 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.

 

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about Windows Phone