代码之家  ›  专栏  ›  技术社区  ›  Matt Casto

从代码获取Windows Phone 7应用程序标题

  •  10
  • Matt Casto  · 技术社区  · 14 年前

    我想从我的ViewModel代码访问存储在wmapmanifest.xml文件中的标题值。这与通过项目属性设置的应用程序标题相同。

    有没有一种方法可以通过类似app.current的代码来访问这个?

    5 回复  |  直到 12 年前
        1
  •  12
  •   Michael S. Scherotter    14 年前

    the source code 对于中的wp7dataCollector.getAppAttribute()。 Microsoft Silverlight Analytics Framework . GetAppAttribute(“标题”)将执行此操作。

        /// <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);
            }
        }
    
        2
  •  1
  •   jyavenard    13 年前

    最后一个答案对我来说似乎过于复杂;你可以简单地做如下的事情:

                    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;
                    }
    
        3
  •  1
  •   Koen Zomers    12 年前

    我已经使用了Michael S.Scherotter他出色的代码示例来计算出一个完全有效的代码示例:

    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";
    
            #endregion
    
            #region Properties
    
            /// <summary>
            /// Gets the application title
            /// </summary>
            public static string Title
            {
                get { return GetAppAttribute("Title"); }
            }
    
            /// <summary>
            /// Gets the application description
            /// </summary>
            public static string Description
            {
                get { return GetAppAttribute("Description"); }
            }
    
            /// <summary>
            /// Gets the application version
            /// </summary>
            public static string Version
            {
                get { return GetAppAttribute("Version"); }
            }
    
            /// <summary>
            /// Gets the application publisher
            /// </summary>
            public static string Publisher
            {
                get { return GetAppAttribute("Publisher"); }
            }
    
            /// <summary>
            /// Gets the application author
            /// </summary>
            public static string Author
            {
                get { return GetAppAttribute("Author"); }
            }
    
            #endregion
    
            #region Methods        
    
            /// <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)
            {
                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
        }
    }
    
        4
  •  1
  •   Tony Wall    12 年前

    只有前两个答案在原问题范围内是正确的。第二个当然不会过于复杂。将helper方法包装为每个可能属性的一个类是面向对象的良好开发,这正是Microsoft在整个框架中所做的,例如由Visual Studio生成的设置设计器文件。

    如果你只想要一个特定的属性,我建议使用第一个,如果你想要更多,我建议使用第二个。应该是SDK的一部分。我们试图在此处读取wmapmanifest.xml,而不是assemblyinfo,因此标准程序集反射元数据不好。

    顺便说一下,如果您真的想从程序集属性(而不是wpappmanifest.xml)中获取产品名称,那么最后一个示例读取的属性是错误的!使用assemblyProductAttribute而不是assemblyTitleAttribute。程序集标题实际上是文件标题,默认情况下与程序集文件名相同(例如mycompany.my product.winphone7app),而产品通常类似于商店中应用程序的格式正确的“标题”(例如“我的产品”)。在使用vs属性页之后,它甚至可能不是最新的,所以您应该检查一下。

    我对所有其他应用程序类型使用assemblyinfo反射在about页面上显示官方产品名称和构建版本,这当然是正确的。但是对于这些特殊的电话应用程序类型,商店清单有更多的重要性和其他属性,你可能需要。

        5
  •  0
  •   Hamzeh Soboh    12 年前

    所有这些答案的问题在于,每次访问该文件时,它们都必须读取该文件。这对性能不好,因为如果经常使用电池,需要考虑电池问题。Koen更接近于一个合适的解决方案,但是每次你想要访问这个值时,他的设计仍然回到了文件中。

    下面的解决方案是对文件的读取。因为它不太可能改变,所以没有理由继续回到过去。在初始化静态类时读取属性,只需最少的麻烦。

    I created this Gist to demonstrate .

    嗯!