Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get my Windows Store app's title and version info?

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

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

XmlResolver
XmlXapResolver
args.ExceptionObject

(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")...

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?

like image 551
B. Clay Shannon-B. Crow Raven Avatar asked Jan 17 '13 02:01

B. Clay Shannon-B. Crow Raven


1 Answers

Let me first address your issues:

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

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

like image 63
Damir Arh Avatar answered Oct 21 '22 09:10

Damir Arh