Get the Windows Phone 7 Application Title from Code

后端 未结 5 693
失恋的感觉
失恋的感觉 2020-12-28 21:27

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 prop

5条回答
  •  庸人自扰
    2020-12-28 22:02

    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
    {
        /// 
        /// Allows application information to be retrieved
        /// 
        public static class ApplicationInfo
        {
            #region Constants
    
            /// 
            /// Filename of the application manifest contained within the XAP file
            /// 
            private const string AppManifestName = "WMAppManifest.xml";
    
            /// 
            /// Name of the XML element containing the application information
            /// 
            private const string AppNodeName = "App";
    
            #endregion
    
            #region Properties
    
            /// 
            /// Gets the application title
            /// 
            public static string Title
            {
                get { return GetAppAttribute("Title"); }
            }
    
            /// 
            /// Gets the application description
            /// 
            public static string Description
            {
                get { return GetAppAttribute("Description"); }
            }
    
            /// 
            /// Gets the application version
            /// 
            public static string Version
            {
                get { return GetAppAttribute("Version"); }
            }
    
            /// 
            /// Gets the application publisher
            /// 
            public static string Publisher
            {
                get { return GetAppAttribute("Publisher"); }
            }
    
            /// 
            /// Gets the application author
            /// 
            public static string Author
            {
                get { return GetAppAttribute("Author"); }
            }
    
            #endregion
    
            #region Methods        
    
            ///  
            /// Gets an attribute from the Windows Phone App Manifest App element 
            ///  
            /// the attribute name 
            /// the attribute value 
            private static string GetAppAttribute(string attributeName)
            {
                var settings = new XmlReaderSettings {XmlResolver = new XmlXapResolver()};
    
                using (var rdr = XmlReader.Create(AppManifestName, settings))
                {
                    rdr.ReadToDescendant(AppNodeName);
    
                    // Return the value of the requested XML attribute if found or NULL if the XML element with the application information was not found in the application manifest
                    return !rdr.IsStartElement() ? null : rdr.GetAttribute(attributeName);
                }
            }
    
            #endregion
        }
    }
    

提交回复
热议问题