我遇到了这样的问题: Forms.context is obsolete.Context从2.5版开始就过时了,请使用本地上下文。
我正在尝试使用Azure Active Directory登录,代码如下。请帮帮忙。
using Xamarin.Forms;
using myMobile.Service;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading.Tasks;
[assembly: Dependency(typeof(myMobile.Droid.Authenticator))]
namespace myMobile.Droid
{
class Authenticator: IAuthenticator
{
public async Task<AuthenticationResult> Authenticate(string tenantUrl, string graphResourceUri, string ApplicationID, string returnUri)
{
try
{
var authContext = new AuthenticationContext(tenantUrl);
if (authContext.TokenCache.ReadItems().Any())
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().FirstOrDefault().Authority);
var authResult = await authContext.AcquireTokenAsync(graphResourceUri, ApplicationID, new Uri(returnUri), new PlatformParameters((Activity)Forms.Context));
return authResult;
}
catch (Exception)
{
return null;
}
}
}
}
// err encountered on this line :(Activity)Forms.Context)
Forms.context is obsolete.Context is obsolete as of version2.5,please use a local context instead.
var authResult = await authContext.AcquireTokenAsync(graphResourceUri, ApplicationID, new Uri(returnUri), new PlatformParameters((Activity)Forms.Context));//-更新:
//-------- Login Page:
private async void Login()
{
try
{
var data = await DependencyService.Get<IAuthenticator>()
.Authenticate(AzureSettings.tenanturl, AzureSettings.GraphResourceUri, AzureSettings.ApplicationID, AzureSettings.ReturnUri);
AzureSettings.AuthenticationResult = data;
//NavigateTopage(data);
}
catch (Exception)
{ }
}
}
//--------- in Shared Project :
//-- interface: IAuthenticator
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading.Tasks;
namespace myMobile.Service
{
public interface IAuthenticator
{
Task<AuthenticationResult> Authenticate(string tenantUrl, string graphResourceUri, string ApplicationID, string returnUri);
}
}
//-------- in Android Project: add
1) Class : Authenticator.cs
using Android.App;
using Android.Content;
using Xamarin.Forms;
using myMobile.Service;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading.Tasks;
[assembly: Dependency(typeof(myMobile.Droid.Authenticator))]
namespace myMobile.Droid
{
class Authenticator: IAuthenticator
{
private readonly Context _context;
public static void Init(Context context)
{
_context = context; //--error
}
public async Task<AuthenticationResult> Authenticate(string tenantUrl, string graphResourceUri, string ApplicationID, string returnUri)
{
try
{
var authContext = new AuthenticationContext(tenantUrl);
if (authContext.TokenCache.ReadItems().Any())
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().FirstOrDefault().Authority);
var authResult = await authContext.AcquireTokenAsync(graphResourceUri, ApplicationID, new Uri(returnUri), new PlatformParameters((Activity) _context));
return authResult;
}
catch (Exception)
{
return null;
}
}
}
}
error :
An Object reference is required for non-static field,method or property.Authenticator._context
//------- Class: MainActivity
namespace myMobile.Droid
{
[Activity(Label = "myMobile", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
DependencyService.Get<IAuthenticator>().Init(this); //<-- Error
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}
Error Message:
IUAthenticator does not contain a definition for Init and no extension method accepting
a first argument of type IAuthenticator发布于 2018-06-21 14:42:31
现在,您必须实现一个自定义构造函数,该构造函数接受一个Context,将其放入一个局部变量中,并使用该构造函数而不是这个,例如new PlatformParameters((Activity)Forms.Context)。
对于自定义渲染器,可以使用下面的解决方案。如下所示:
public MyControlRenderer : ControlRenderer
{
private readonly Context _context;
public MyControlRenderer(Context context) : base(context)
{
_context = context;
}
}对于像您这样的依赖项服务,您必须找到一种方法来提供Context。由于Xamarin.Forms使用单个活动,因此您可以使用某种初始化方法。
将以下代码添加到您的代码中:
public class MyService : IMyService
{
private static Context _context;
public static void Init(Context context)
{
_context = context;
}
}现在从你的MainActivity调用Init,之后你应该会做得很好。也是如此:DependencyService.Get<IMyService>().Init(this);
对于在多个活动中遇到这种情况的其他人,请参考这里的文档:https://www.davidbritch.com/2017/11/xamarinforms-25-and-local-context-on.html就是本文的灵感来源。
https://stackoverflow.com/questions/50961802
复制相似问题