C# uwp launch apps

六月ゝ 毕业季﹏ 提交于 2019-12-06 11:21:16

If you want to launch one app from another, the target app must have registered URI activation and handle that case. More about that you can read at MSDN.

Lots of apps in the store has registered URI scheme, there are some lists over the internet, like this one, however I'm not sure if it's actual and which apps work with UWP.

Or, if there is another way to check if App is installed, if it is then launch it, if it is not then show it in store, so user can install it manually.

You could call Launcher.QueryUriSupportAsync to see if the app is installed. This method will return LaunchQuerySupportStatus enumeration value, you could decide to open the app or windows store by this value.

Please check the following code for details:

var ret = await Windows.System.Launcher.QueryUriSupportAsync(new Uri("fb:post?text=foo"), Windows.System.LaunchQuerySupportType.Uri);
if (ret == LaunchQuerySupportStatus.Available)
{
    await Windows.System.Launcher.LaunchUriAsync(new Uri("fb:post?text=foo"));
}
else
{
    await Windows.System.Launcher.LaunchUriAsync(new Uri(@"ms-windows-store://pdp/?ProductId=9wzdncrfj2wl"));
}

It is possible by using Package Manager:

using Windows.Management.Deployment;

var app = await GetAppByPackageFamilyNameAsync("Microsoft.WindowsCalculator_8wekyb3d8bbwe");

if(app != null)
{
  await app.LaunchAsync();
}    

static async Task<AppListEntry> GetAppByPackageFamilyNameAsync(string packageFamilyName)
{
    var pkgManager = new PackageManager();
    var pkg = pkgManager.FindPackagesForUser("", packageFamilyName).FirstOrDefault();

    if (pkg == null) return null;

    var apps = await pkg.GetAppListEntriesAsync();
    var firstApp = apps.FirstOrDefault();
    return firstApp;
}

And add one capability to the Package.appxmanifest :

<?xml version="1.0" encoding="utf-8"?>    
<Package xmlns:...
         xmlns:rescap = "http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 
         IgnorableNamespaces="... rescap">
  ...
  <Capabilities>
    ...
    <rescap:Capability Name="packageQuery" />
  </Capabilities>
</Package>

Learn more about restricted capabilities: https://docs.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations#restricted-capabilities

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