xamarin - 从共享中调用特定的 iOS/Android 例程
问题描述
我有一段在iOS中运行的代码:
namespace Login.iOS
{
class Authenticate
{
public async Task LoginAsync()
{
var client = new Auth0Client(new Auth0ClientOptions
{
Domain = "difiore.auth0.com",
ClientId = "Key goes here"
});
var loginResult = await client.LoginAsync();
}
}
}
以及它对Android的等价物:
namespace Login.Droid
{
class Authenticate
{
public async Task LoginAsync()
{
var client = new Auth0Client(new Auth0ClientOptions
{
Domain = "difiore.auth0.com",
ClientId = "Key goes here"
});
var loginResult = await client.LoginAsync();
}
}
}
由于两者都依赖于专门为平台编写的库,因此它们必须是特定于平台的。
然后我有应该调用的共享代码:
namespace Login
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
public MainPage ()
{
InitializeComponent ();
}
private void Login_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Authenticate());
}
}
}
但是代码行
Navigation.PushAsync(new Authenticate());
显然是错误的,因为它没有识别要调用的例程。
如何从共享的程序中调用特定于平台的例程?那是IOS和Android中的Authenticate类。
解决方案
您必须创建一个接口,以便使用Dependency Injection在共享或可移植类中调用平台特定类。按照此链接 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction。我希望这有帮助。
推荐阅读
- python - 尝试获取字数时此 PDF 有什么问题
- node.js - axios/fetch 和 react-native 的网络错误
- html - CSS :hover 伪类在我的代码中不起作用
- xero-api - FullPaidOnDate 不同时检索单个发票与发票集合分配信用票据时
- r - 我想知道如何删除我的 ggplot 条形图的 NA 部分并向其添加数字标签
- python - 我可以在代码中的 python 游戏中包含音乐吗?
- javascript - 如何在 HTML 文件中运行 NodeJS
- html - 有没有办法在不访问服务器的情况下从网站 url 编辑/删除 url 文件名(尤其是 .html 扩展名)?
- javascript - 将数组传递给Javascript函数只传递第一个元素
- python - ModuleNotFoundError:Spyder 上没有名为“selenium”的模块