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

发布于 2024-09-13 03:02:57 字数 119 浏览 4 评论 0原文

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

有没有办法使用 App.Current 之类的代码来访问它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

飘落散花 2024-09-20 03:02:57

查看 源代码 中的 WP7DataCollector.GetAppAttribute() a href="http://msaf.codeplex.com" rel="nofollow noreferrer">Microsoft Silverlight 分析框架。 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);
        }
    }

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);
        }
    }
命比纸薄 2024-09-20 03:02:57

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

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

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;
                }
_蜘蛛 2024-09-20 03:02:57

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

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

        #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
    }
}
痕至 2024-09-20 03:02:57

只有前两个答案在原始问题的范围内是正确的。第二个肯定不会太复杂。使用每个可能属性的类包装帮助器方法是良好的面向对象开发,这正是 Microsoft 在整个框架中所做的事情,例如由 Visual Studio 生成的设置设计器文件。

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

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

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

Only the first two answers are correct in scope of the original question. And the second is certainly not over complicated. Wrapping the helper method with a class for each possible attribute is good object orientated development and exactly what Microsoft do all over the framework, e.g. settings designer files generated by Visual Studio.

I'd recommend using the first if you just want one specific property, the second if you want more. Should be part of the SDK really. We're trying to read the WMAppManifest.xml here not the AssemblyInfo so standard assembly reflection metadata is no good.

By the way, if you really want to get the product name from the assembly attributes (not WPAppManifest.xml) then the last sample was reading the wrong attribute! Use the AssemblyProductAttribute not the AssemblyTitleAttribute. The assembly title is really the file title, by default the same as the assembly file name (e.g. MyCompany.MyProduct.WinPhone7App) whereas the product will typically be something like the properly formatted "title" of your app in the store (e.g. "My Product"). It may not even be up-to-date after using the VS properties page, so you should check that.

I use AssemblyInfo reflection for all other application types to show the official product name and build version on an about page, it's certainly correct for that. But for these special phone app types the store manifest has more importance and other attributes you may need.

倾`听者〃 2024-09-20 03:02:57

所有这些答案的问题在于,每次访问该文件时,他们都必须读取该文件。这对性能不利,因为如果您经常使用它,则需要考虑电池问题。 Koen 更接近正确的解决方案,但每次您想要访问该值时,他的设计仍然会返回到文件。

下面的解决方案是一次性读取文件。既然它不太可能改变,就没有理由继续回到它。在初始化静态类时读取属性,无需大惊小怪。

我创建了这个 Gist 来演示

哈!

The problem with all of those answers is that they have to read the file every single time it is accessed. This is bad for performance as there are battery issues to consider if you use it frequently. Koen was closer to a proper solution, but his design still went back to the file every time you wanted to access the value.

The solution below is a one-and-done read of the file. Since it is not likely to change, there is no reason to keep going back to it. The attributes are read as the static class is initialized, with minimal fuss.

I created this Gist to demonstrate.

HTH!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文