C# uwp launch apps

ⅰ亾dé卋堺 提交于 2019-12-07 19:32:55

问题


With this code below:

await Windows.System.Launcher.LaunchUriAsync(new Uri(@"ms-windows-store://review/?ProductId=9wzdncrfj2wl"));

I am opening Facebook app in Microsoft Store. There is a Launch button on that page. With it user runs the application.

How can I run the application with it's product ID?

I managed to open app with this code:

await Windows.System.Launcher.LaunchUriAsync(new Uri("fb:post?text=foo"));

But I want to open it with ID.

I have found this code, but it is not working:

await Windows.System.Launcher.LaunchUriAsync(new Uri(@"ms-windows-store://pdp/?ProductId=9wzdncrfj2wl"));

How to open installed app with ID? 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. The app I am developing is Windows 10 UWP...

I have a situation where there is no URI for application, so I have to open it via it's ProductID or ProductFamily...

So, this is the shortcut's target that is opening that app: C:\Windows\explorer.exe shell:AppsFolder\A88BB54F.N1info_gvc78jvcn5cg0!App

Is there any chance I can use this in UWP app to launch app?

Does anyone got the link from LAUNCH button in Windows Store? So, I would put that in URI, just like link from GET button:

ms-windows-store:PDP?PFN=A88BB54F.N1info_gvc78jvcn5cg0&referrer=unistoreweb&webig=39694073-f9af-436f-a82b-abb9d9f644f0&muid=097C7AA3CA2C6EE22D237359CE2C689A&websession=c9916902dd014ec2b5a9e0390a28c26d

I am using it like this:

await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:PDP?PFN=A88BB54F.N1info_gvc78jvcn5cg0&referrer=unistoreweb&webig=39694073-f9af-436f-a82b-abb9d9f644f0&muid=097C7AA3CA2C6EE22D237359CE2C689A&websession=c9916902dd014ec2b5a9e0390a28c26d"));

and it is showing app in store.

Thanx.


回答1:


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.




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/41979131/c-sharp-uwp-launch-apps

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