Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 791
Matt Casto Avatar asked Aug 05 '10 02:08

Matt Casto


2 Answers

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);
        }
    }
like image 165
Michael S. Scherotter Avatar answered Oct 04 '22 03:10

Michael S. Scherotter


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;
                }
like image 25
jyavenard Avatar answered Oct 04 '22 05:10

jyavenard