本文介绍了如何从Xamarin.Forms内部启动外部应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题标题所暗示的那样,我正在寻找一种从 Xamarin.Forms 应用程序内部启动外部应用程序的方法.例如,我的应用程序有一个地址列表,当用户点击其中一个地址时,将打开当前平台的内置地图应用程序(Android的Google Maps,iOS的Apple Maps).万一重要,我只针对Android和iOS.

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.

我当然可以使用依赖项服务,并在每个平台的基础上编写应用程序启动代码,但是我更愿意只编写一次. Xamarin.Forms中有本地方法可以做到这一点吗?我找不到在Xamarin网站或论坛上正式记录下来的任何内容.

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.

推荐答案

使用 Device.OpenUri 并将其传递给适当的URI,并与 Device.OnPlatform 来格式化每个平台的URI

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

这篇关于如何从Xamarin.Forms内部启动外部应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 00:48