问题
As the question title suggests, I'm looking for a way to launch an external app from within a Xamarin.Forms app. For example, my app has a list of addresses, and when the user taps on one of them, the built-in map app for the current platform would open (Google Maps for Android, Apple Maps for iOS). In case it matters, I am only targeting Android and iOS.
I could use a dependency service and write the app-launching code on a per-platform basis, of course, but I'd prefer if I only had to write it once. Is there a native way to do this in Xamarin.Forms? I was unable to find anything that officially documented this on the Xamarin site or forums.
回答1:
Use Device.OpenUri and pass it the appropriate URI, combined with Device.OnPlatform to format the URI per platform
string url;
Device.OnPlatform(iOS: () =>
{
url = String.Format("http://maps.apple.com/maps?q={0}", address);
},
Android: () =>
{
url = String.Format("http://maps.google.com/maps?q={0}", address);
});
Device.OpenUri(url);
回答2:
As of Xamarin.Forms v4.3.0.908675 (probably v4.3.0) Device.OpenUri
is deprecated and you should use Launcher.TryOpenAsync
from Xamarin.Essentials instead. Although you will find it troublesome, or at least I did.
来源:https://stackoverflow.com/questions/34643604/how-can-i-natively-launch-an-external-app-from-within-xamarin-forms