如何从 Xamarin.Forms 中本地启动外部应用程序?
How can I natively launch an external app from within Xamarin.Forms?
如问题标题所示,我正在寻找一种从 Xamarin.Forms 应用程序中启动外部应用程序的方法。例如,我的应用程序有一个地址列表,当用户点击其中一个时,当前平台的 built-in 地图应用程序将打开(Google Android 的地图,Apple iOS 的地图)。以防万一,我只针对 Android 和 iOS。
当然,我可以使用依赖服务并在 per-platform 的基础上编写 app-launching 代码,但我更愿意只编写一次。在 Xamarin.Forms 中是否有本地方法可以做到这一点?我无法在 Xamarin 站点或论坛上找到任何对此进行正式记录的内容。
使用 Device.OpenUri and pass it the appropriate URI, combined with 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);
从 Xamarin.Forms v4.3.0.908675(可能是 v4.3.0)开始,Device.OpenUri
已弃用,您应该改用 Xamarin.Essentials 中的 Launcher.TryOpenAsync
。虽然你会觉得很麻烦,至少我是这样。
使用下面的代码,并添加命名空间 Xamarin.Essentials
和 Xamarin.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");
}
如问题标题所示,我正在寻找一种从 Xamarin.Forms 应用程序中启动外部应用程序的方法。例如,我的应用程序有一个地址列表,当用户点击其中一个时,当前平台的 built-in 地图应用程序将打开(Google Android 的地图,Apple iOS 的地图)。以防万一,我只针对 Android 和 iOS。
当然,我可以使用依赖服务并在 per-platform 的基础上编写 app-launching 代码,但我更愿意只编写一次。在 Xamarin.Forms 中是否有本地方法可以做到这一点?我无法在 Xamarin 站点或论坛上找到任何对此进行正式记录的内容。
使用 Device.OpenUri and pass it the appropriate URI, combined with 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);
从 Xamarin.Forms v4.3.0.908675(可能是 v4.3.0)开始,Device.OpenUri
已弃用,您应该改用 Xamarin.Essentials 中的 Launcher.TryOpenAsync
。虽然你会觉得很麻烦,至少我是这样。
使用下面的代码,并添加命名空间 Xamarin.Essentials
和 Xamarin.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");
}