[英]How can I natively launch an external app from within Xamarin.Forms?
正如問題標題所暗示的那樣,我正在尋找一種從Xamarin.Forms應用程序中啟動外部應用程序的方法。 例如,我的應用程序有一個地址列表,當用戶點擊其中一個時,當前平台的內置地圖應用程序將打開(Android 版 Google 地圖,iOS 版 Apple 地圖)。 以防萬一,我只針對 Android 和 iOS。
當然,我可以使用依賴服務並在每個平台的基礎上編寫應用程序啟動代碼,但我更願意只需要編寫一次。 在 Xamarin.Forms 中是否有一種本地方法可以做到這一點? 我無法在 Xamarin 網站或論壇上找到任何正式記錄此內容的內容。
使用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);
從 Xamarin.Forms v4.3.0.908675(可能是 v4.3.0) Device.OpenUri
,不推薦使用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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.