Saturday, September 1, 2012

Get the Windows Phone 7 Application Title from Code

Get the Windows Phone 7 Application Title from Code

I want to access the Title value that is stored in the WMAppManifest.xml file from my ViewModel code. This is the same application title that is set through the project properties.

Is there a way to access this from code using something like App.Current?

Answers & Comments...

Answer: 1

Look at the source code for WP7DataCollector.GetAppAttribute() in the Microsoft Silverlight Analytics Framework. GetAppAttribute("Title") will do it.

    /// <summary>     /// Gets an attribute from the Windows Phone App Manifest App element     /// </summary>     /// <param name="attributeName">the attribute name</param>     /// <returns>the attribute value</returns>     private static string GetAppAttribute(string attributeName)     {         string appManifestName = "WMAppManifest.xml";         string appNodeName = "App";          var settings = new XmlReaderSettings();         settings.XmlResolver = new XmlXapResolver();          using (XmlReader rdr = XmlReader.Create(appManifestName, settings))         {             rdr.ReadToDescendant(appNodeName);             if (!rdr.IsStartElement())             {                 throw new System.FormatException(appManifestName + " is missing " + appNodeName);             }              return rdr.GetAttribute(attributeName);         }     } 
by : Michael S. Scherotterhttp://stackoverflow.com/users/27306

Answer: 2

This last answer seems overly complicated to me ; you could have simply done something like:

                string name = "";                 var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();                 var customAttributes = executingAssembly.GetCustomAttributes(typeof(System.Reflection.AssemblyTitleAttribute), false);                 if (customAttributes != null)                 {                     var assemblyName = customAttributes[0] as System.Reflection.AssemblyTitleAttribute;                     name = assemblyName.Title;                 } 
by : jyavenardhttp://stackoverflow.com/users/298699

Answer: 3

I have used Michael S. Scherotter his excellent code sample to work it out to a fully working code sample:

using System.Xml;  namespace KoenZomers.WinPhone.Samples {     /// <summary>     /// Allows application information to be retrieved     /// </summary>     public static class ApplicationInfo     {         #region Constants          /// <summary>         /// Filename of the application manifest contained within the XAP file         /// </summary>         private const string AppManifestName = "WMAppManifest.xml";          /// <summary>         /// Name of the XML element containing the application information         /// </summary>         private const string AppNodeName = "App";          

No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog