简体   繁体   English

如何从 Xamarin.Forms 内本机启动外部应用程序?

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

As the question title suggests, I'm looking for a way to launch an external app from within a Xamarin.Forms app.正如问题标题所暗示的那样,我正在寻找一种从Xamarin.Forms应用程序中启动外部应用程序的方法。 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).例如,我的应用程序有一个地址列表,当用户点击其中一个时,当前平台的内置地图应用程序将打开(Android 版 Google 地图,iOS 版 Apple 地图)。 In case it matters, I am only targeting Android and iOS.以防万一,我只针对 Android 和 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?在 Xamarin.Forms 中是否有一种本地方法可以做到这一点? I was unable to find anything that officially documented this on the Xamarin site or forums.我无法在 Xamarin 网站或论坛上找到任何正式记录此内容的内容。

Use Device.OpenUri and pass it the appropriate URI, combined with Device.OnPlatform to format the URI per platform使用Device.OpenUri并将适当的 URI 传递给它,结合Device.OnPlatform以格式化每个平台的 URI

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

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.从 Xamarin.Forms v4.3.0.908675(可能是 v4.3.0) Device.OpenUri ,不推荐使用Device.OpenUri ,您应该改用 Xamarin.Essentials 中的Launcher.TryOpenAsync Although you will find it troublesome, or at least I did.虽然你会觉得它很麻烦,至少我做到了。

Use Below code, and add namespaces Xamarin.Essentials and Xamarin.Forms使用下面的代码,并添加命名空间Xamarin.EssentialsXamarin.Forms

if (Device.RuntimePlatform == Device.iOS)
{
    // https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
    await Launcher.OpenAsync("http://maps.apple.com/?daddr=San+Francisco,+CA&saddr=cupertino");
}
else if (Device.RuntimePlatform == Device.Android)
{
    // opens the 'task chooser' so the user can pick Maps, Chrome or other mapping app
    await Launcher.OpenAsync("http://maps.google.com/?daddr=San+Francisco,+CA&saddr=Mountain+View");
}
else if (Device.RuntimePlatform == Device.UWP)
{
    await Launcher.OpenAsync("bingmaps:?rtp=adr.394 Pacific Ave San Francisco CA~adr.One Microsoft Way Redmond WA 98052");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Xamarin.Forms 中启动谷歌照片应用程序? - How can I launch google photos app from within Xamarin.Forms? 如何在应用中心的 Xamarin.Forms 中使用设置环境变量 - How can I use set Environment Variables in Xamarin.Forms from App Center 如何从 Xamarin.Forms 获得连续的地理定位? - How can I get a continous geolication from Xamarin.Forms? Xamarin.Forms UWP-登录时启动应用程序 - Xamarin.Forms UWP - Launch app on login 如何允许用户在Xamarin.Forms的ListView中平移地图? - How can I allow a user to pan a map within a ListView in Xamarin.Forms? How can I restrict my Azure Web App API access to only my Xamarin.Forms app? - How can I restrict my Azure Web App API access to only my Xamarin.Forms app? 无法从Xamarin.Forms应用连接到SignalR服务器 - Can't connect to SignalR server from Xamarin.Forms app 如何刷新 Xamarin.Forms 应用程序上的选项卡 - How do I refresh a tab on Xamarin.Forms app 我如何在xamarin.forms应用程序中使用permissionplugin和zxing nuget pkg? - How i can use permissionplugin with zxing nuget pkg in xamarin.forms app? 如何截取我的 Xamarin.Forms 应用程序的部分屏幕截图并将其保存到我的库中? - How can I take a partial screenshot of my Xamarin.Forms app and save it into my gallery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM