Android 在Xamarin中创建特定于平台的代码?
因此,我想在我的Xamarin应用程序中实现twitter登录,但如果我遵循教程,便携应用程序将不允许我执行此功能:Android 在Xamarin中创建特定于平台的代码?,android,authentication,xamarin,Android,Authentication,Xamarin,因此,我想在我的Xamarin应用程序中实现twitter登录,但如果我遵循教程,便携应用程序将不允许我执行此功能: var ui = auth.GetUI(this); 当auth为此时: var auth = new OAuth1Authenticator( "DynVhdIjJDXXXXXXXXXXXXX", "REvU5dCUQI4MvjV6aWwXXXXXXXXXXXXXXXXXXXXXXX", new Uri("https://api.twitter.com/oauth
var ui = auth.GetUI(this);
当auth为此时:
var auth = new OAuth1Authenticator(
"DynVhdIjJDXXXXXXXXXXXXX",
"REvU5dCUQI4MvjV6aWwXXXXXXXXXXXXXXXXXXXXXXX",
new Uri("https://api.twitter.com/oauth/request_token"),
new Uri("https://api.twitter.com/oauth/authorize"),
new Uri("https://api.twitter.com/oauth/access_token"),
new Uri("http://twitter.com"));
所以我需要将它添加到Android项目中,但是如何从那里显示按钮
谢谢你的帮助!:)
在Xamarin中创建特定于平台的代码
您可以使用DependencyService
来实现此功能。这是和。该文件解释说:
DependencyService允许应用程序从共享代码调用特定于平台的功能。此功能使Xamarin.Forms应用程序能够执行本机应用程序可以执行的任何操作
在PCL中创建ILogin
接口:
public interface ILogin
{
void GetUI();
}
在Xamarin.Android中实现接口:
[assembly: Xamarin.Forms.Dependency(typeof(TwitterLogin))]
namespace TwitterDemo.Droid
{
public class TwitterLogin : ILogin
{
public TwitterLogin()
{
}
public void GetUI()
{
var auth = new OAuth1Authenticator("DynVhdIjJDXXXXXXXXXXXXX", "REvU5dCUQI4MvjV6aWwXXXXXXXXXXXXXXXXXXXXXXX",
new Uri("https://api.twitter.com/oauth/request_token"),
new Uri("https://api.twitter.com/oauth/authorize"),
new Uri("https://api.twitter.com/oauth/access_token"),
new Uri("http://twitter.com"));
Intent intent = auth.GetUI(Android.App.Application.Context);
Forms.Context.StartActivity(intent);
}
}
}
在Xamarin中使用它。形式:
private void Button_Clicked(object sender, EventArgs e)
{
Xamarin.Forms.DependencyService.Register<ILogin>();
DependencyService.Get<ILogin>().GetUI();
}
private void按钮\u已单击(对象发送者,事件参数e)
{
Xamarin.Forms.DependencyService.Register();
DependencyService.Get().GetUI();
}
基于OAuth1Authenticator
和GetUI()
我假设您使用Xamarin.Auth
Xamarin.Auth支持Xamarin.Forms,它有两个实现,分别用于GetUI()
、所谓的Presenter(实际上是依赖项服务/注入)和CustomRenderer。演示者经过了更多的测试(因此有更多的稳定性证据),但您可以在repo中检查代码
示例如何将Xamarin.Auth与Xamarin.Forms一起使用:
private void Button_Clicked(object sender, EventArgs e)
{
Xamarin.Forms.DependencyService.Register<ILogin>();
DependencyService.Get<ILogin>().GetUI();
}
注意:此repo包含从主Xamarin.Auth repo中提取的样本,因此处理更容易,样本更新更频繁。您是否使用RestSharp OAuth1Authenticator?便携应用程序无法处理UI代码。您需要在portable类项目中使用接口,在android中使用实现类project@MalteGoetz对