如何获取我的 Windows 应用商店应用的标题和版本信息? [英] How can I get my Windows Store app's title and version info?

查看:21
本文介绍了如何获取我的 Windows 应用商店应用的标题和版本信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在我的 WP8 应用中运行良好:

This code works fine in my WP8 app:

void App_UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
    string appName;
    string appVersion;
    var xmlReaderSettings = new XmlReaderSettings
    {
        XmlResolver = new XmlXapResolver()
    };

    using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
    {
        xmlReader.ReadToDescendant("App");

        appName = xmlReader.GetAttribute("Title");
        appVersion = xmlReader.GetAttribute("Version");
    }

    WAMS_EXCEPTIONLOG wamsel = new WAMS_EXCEPTIONLOG
    {
        appNameAndVersion =
            string.Format("{0} {1}", appName,
                          appVersion),
        ExceptionMsg =
            args.ExceptionObject.Message,
        InnerException =
            args.ExceptionObject
                .InnerException.ToString(),
        ExceptionToStr =
            args.ExceptionObject.ToString(),
        dateTimeOffsetStamp =
            DateTimeOffset.UtcNow
    };
    await MobileService.GetTable<TASLS_WAMS_EXCEPTIONLOG>().InsertAsync(wamsel);
}

...但在我的补充 Windows 商店应用程序中,几个类和类成员无法识别,即:

...but in my complementary Windows store app, several classes and class members are unrecognized, to wit:

XmlResolver
XmlXapResolver
args.ExceptionObject

(更不用说不允许 await,并且向事件处理程序添加async"会导致事件处理程序的分配变红")...

(not to mention the fact that await is not allowed, and adding "async" to the event handler causes the assignment of the event handler to "go red")...

那么,回到主要观点:我如何才能通过 Windows Store 应用程序实现与 WP8 应用程序相同的功能?

So, to get back to the main point: How can I achieve the same functionality I'm getting with my WP8 app with my Windows Store app?

推荐答案

让我先解决您的问题:

  • 无需直接从 XML 读取包信息,您可以使用 PackageId 类.
  • 异常信息存储在 args.Exception 中.
  • 您可以通过将 async void 放在方法签名中来从事件处理程序调用异步方法,但您必须记住,该方法将在即发即忘"模式下调用,即应用程序不会等待异步方法完成.如果您设置 args.Handled = true 从而阻止应用程序关闭,这应该不是问题.
  • There is no need to read the package info directly from XML, you can use the PackageId class instead.
  • Exception info is stored in args.Exception.
  • You can call asynchronous methods from the event handler by putting async void in the method signature but you must keep in mind that the method will be called in "fire and forget" mode, i.e. the app won't wait for the asynchronous method to complete.This shouldn't be a problem if you set args.Handled = true and thus prevent the app from closing.

您的固定事件处理程序应如下所示:

Your fixed event handler should look like this:

private async void App_UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
    string appName = Package.Current.Id.Name;
    var version = Package.Current.Id.Version;
    string appVersion = String.Format("{0}.{1}.{2}.{3}", 
        version.Major, version.Minor, version.Build, version.Revision);

    WAMS_EXCEPTIONLOG wamsel = new WAMS_EXCEPTIONLOG
    {
        appNameAndVersion = string.Format("{0} {1}", appName, appVersion),
        ExceptionMsg = args.Exception.Message,
        InnerException = args.Exception.InnerException.ToString(),
        ExceptionToStr = args.Exception.ToString(),
        dateTimeOffsetStamp = DateTimeOffset.UtcNow
    };
    args.Handled = true;
    await MobileService.GetTable<TASLS_WAMS_EXCEPTIONLOG>().InsertAsync(wamsel);
}

在调用 ToString() 之前,您还应该检查 args.Exception.InnerException 是否为 null.

You should also check if args.Exception.InnerException is null, before calling ToString() on it.

这篇关于如何获取我的 Windows 应用商店应用的标题和版本信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆