我正在使用 xamarin 創建一個演示專案,其中,我想制作一個可以放入 iOS 原生端的函式,并在需要時使用該特定函式,并從 iOS 原生函式中獲取回呼。我已經檢查過,但沒有找到任何可以與我合作的足夠的東西。我只想為 iOS 呼叫下面的代碼,并且需要它對 xamarin 代碼的回應。
UIApplication.shared.open(redirectUrl, options: [:], completionHandler: { success in
if success {
// Handle success.
} else {
// Handle failure. Most likely app is not present on the user's device. You can redirect to App Store using these links:
}
})
我愿意接受任何可以解決我呼叫此函式的問題的方法。如果我可以在 xamarin 中呼叫它,那么也請給我建議,以便我可以將它放入 xamarin 檔案中。
uj5u.com熱心網友回復:
如果我理解正確,這可以通過依賴服務來實作。
表單中的界面
public interface IMyService
{
Task<bool> openUrl(string url);
}
在 iOS 中實作
[assembly: Dependency(typeof(MyForms.iOS.MyService))]
namespace MyForms.iOS
{
internal class MyService : IMyService
{
public Task<bool> openUrl(string url)
{
return UIApplication.SharedApplication.OpenUrlAsync(new NSUrl(url), null);
}
}
}
表格中的用法
bool success = await DependencyService.Get<IMyService>().openUrl("www.google.com");
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437389.html
