从代码中获取 Windows Phone 7 应用程序标题
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 源代码 中的 WP7DataCollector.GetAppAttribute() a href="http://msaf.codeplex.com" rel="nofollow noreferrer">Microsoft Silverlight 分析框架。 GetAppAttribute("Title") 就可以了。
Look at the source code for WP7DataCollector.GetAppAttribute() in the Microsoft Silverlight Analytics Framework. GetAppAttribute("Title") will do it.
最后一个答案对我来说似乎过于复杂;你可以简单地做类似的事情:
This last answer seems overly complicated to me ; you could have simply done something like:
我使用了 Michael S. Scherotter 他优秀的代码示例来将其制作成一个完全工作的代码示例:
I have used Michael S. Scherotter his excellent code sample to work it out to a fully working code sample:
只有前两个答案在原始问题的范围内是正确的。第二个肯定不会太复杂。使用每个可能属性的类包装帮助器方法是良好的面向对象开发,这正是 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.
所有这些答案的问题在于,每次访问该文件时,他们都必须读取该文件。这对性能不利,因为如果您经常使用它,则需要考虑电池问题。 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!