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

10

我希望能够从我的ViewModel代码中访问存储在WMAppManifest.xml文件中的Title值。这是通过项目属性设置的应用程序标题。

是否有一种使用类似于App.Current的方法可以从代码中访问它?

5个回答

12

查看Microsoft Silverlight Analytics Framework中WP7DataCollector.GetAppAttribute()的源代码。使用GetAppAttribute("Title")即可。

    /// <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);
        }
    }

1
当我们使用本地化资源:@AppResLib.dll,-101时,此解决方案无法正常工作。 - Vinicius Rocha

1

对我来说,这个最后的答案似乎过于复杂了;你可以简单地做一些像这样的事情:

                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;
                }

1

我使用了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
    }
}

1

只有前两个答案符合原问题的范围。第二个答案并不过于复杂。为每个可能的属性包装帮助方法是良好的面向对象开发,这正是微软在整个框架中所做的,例如由Visual Studio生成的设置设计器文件。

如果您只想要一个特定的属性,则建议使用第一个;如果您想要更多,则使用第二个。这应该是SDK的一部分。我们正在尝试读取WMAppManifest.xml而不是AssemblyInfo,因此标准的程序集反射元数据无法使用。

顺便说一下,如果您真的想从程序集属性中获取产品名称(而不是WPAppManifest.xml),那么最后一个示例读取了错误的属性!请使用AssemblyProductAttribute而不是AssemblyTitleAttribute。程序集标题实际上是文件标题,默认情况下与程序集文件名相同(例如MyCompany.MyProduct.WinPhone7App),而产品通常是应用商店中格式正确的“标题”(例如“My Product”)。即使在使用VS属性页面后,它也可能不是最新的,因此您应该检查一下。

我使用AssemblyInfo反射来显示关于页面上的官方产品名称和构建版本,对于其他所有应用程序类型都是正确的。但是对于这些特殊的手机应用程序类型,商店清单具有更重要的意义和其他你可能需要的属性。


0
所有这些答案的问题在于每次访问文件时都必须读取它。如果您经常使用它,这对性能不利,因为需要考虑电池问题。Koen更接近正确的解决方案,但他的设计仍然每次访问值时都会返回到文件。
下面的解决方案是对文件进行一次性读取。由于它不太可能改变,所以没有理由继续回到它。属性在静态类初始化时被读取,非常简单。
我创建了这个Gist来演示。
希望对你有帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接