How to get current AppInfo in UWP environment

▼魔方 西西 提交于 2019-12-13 02:55:39

问题


How can I get the current AppInfo of the running UWP application? I could not find any accessible constructor or static function to get this information.


回答1:


Make sure that your device is updated to Windows10 Creators Update version.
Firstly, you should add Capability appDiagnostics to Package.appxmanifest. Directly to modify the code of the file.

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"     xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp rescap">
  <Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="appDiagnostics"/>
  </Capabilities>
</Package>

Then in the main code, get current package by matching the PackageFamilyName.

var list = await AppDiagnosticInfo.RequestInfoAsync();
var currentPackage = list.Where(o => o.AppInfo.PackageFamilyName == Package.Current.Id.FamilyName).FirstOrDefault();
if (currentPackage != null)
{
    AppInfo currentAppInfo = currentPackage.AppInfo;
}



回答2:


You could get App​Diagnostic​Info of all uwp apps that are running by the following code.

var list = await App​Diagnostic​Info.RequestInfoAsync();

And then you could get AppInfo from each item in the App​Diagnostic​Info list.

foreach (var diagnosticinfo in list)
{
    info = diagnosticinfo.AppInfo;
}
var id = info.Id;

Please note that you need to declare the following capabilities in the Package.appxmainfest.

<Package
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 
IgnorableNamespaces="uap mp rescap">
  <Capabilities>
    <rescap:Capability Name="appDiagnostics" />
  </Capabilities>
</Package>


来源:https://stackoverflow.com/questions/44299452/how-to-get-current-appinfo-in-uwp-environment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!