8

Questo codice funziona bene nel mio WP8 app:Come posso ottenere informazioni sul titolo e sulla versione dell'app di Windows Store?

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

... ma nella mia complementare app store di Windows, diverse classi e membri della classe sono riconosciuti, vale a dire:

XmlResolver 
XmlXapResolver 
args.ExceptionObject 

(non menzionare il fatto che attendere non è consentito e aggiungere "async" al gestore eventi causa l'assegnazione del gestore eventi a "go red") ...

Quindi, per tornare al punto principale: come può Ottengo la stessa funzionalità che sto ottenendo con la mia app WP8 con la mia app di Windows Store?

risposta

17

Vorrei innanzi tutto affrontare i vostri problemi:

  • Non c'è bisogno di leggere l'informazioni sul pacchetto direttamente da XML, è possibile utilizzare la PackageId class invece.
  • Le informazioni di eccezione sono memorizzate in args.Exception.
  • È possibile chiamare i metodi asincroni dal gestore eventi inserendo async void nella firma del metodo, ma è necessario tenere presente che il metodo verrà chiamato in modalità "ignora e dimentica", ovvero l'app non attenderà il metodo asincrono per completare.Questo non dovrebbe essere un problema se si imposta args.Handled = true e quindi impedire la chiusura dell'app.

vostro gestore di eventi fisso dovrebbe essere simile a questo:

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

Si dovrebbe anche controllare se args.Exception.InnerException è null, prima di chiamare ToString() su di esso.

+0

+1 su questa risposta e sull'uso astuto delle nuove API Package.Current. – JustinAngel

+1

'Package.Current.Id' genera' NotImplementedException' su Windows Phone 8 –

+1

@ Cœur Per windows phone 8, si dovrebbe ottenere come questo. 'String version = XDocument.Load (" WMAppManifest.xml "). Root.Element (" App "). Attributo (" Versione "). Valore;' –

Problemi correlati