How can I natively launch an external app from within Xamarin.Forms?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 07:15:30

问题


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

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