首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Forms.context is obsolete.Context从2.5版开始已过时,请改用本地上下文

Forms.context is obsolete.Context从2.5版开始已过时,请改用本地上下文
EN

Stack Overflow用户
提问于 2018-06-21 14:31:55
回答 1查看 2K关注 0票数 1

我遇到了这样的问题: Forms.context is obsolete.Context从2.5版开始就过时了,请使用本地上下文。

我正在尝试使用Azure Active Directory登录,代码如下。请帮帮忙。

代码语言:javascript
运行
复制
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));

//-更新:

代码语言:javascript
运行
复制
 //-------- 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
EN

回答 1

Stack Overflow用户

发布于 2018-06-21 14:42:31

现在,您必须实现一个自定义构造函数,该构造函数接受一个Context,将其放入一个局部变量中,并使用该构造函数而不是这个,例如new PlatformParameters((Activity)Forms.Context)

对于自定义渲染器,可以使用下面的解决方案。如下所示:

代码语言:javascript
运行
复制
public MyControlRenderer : ControlRenderer
{
    private readonly Context _context;

    public MyControlRenderer(Context context) : base(context)
    {
        _context = context;
    }
}

对于像您这样的依赖项服务,您必须找到一种方法来提供Context。由于Xamarin.Forms使用单个活动,因此您可以使用某种初始化方法。

将以下代码添加到您的代码中:

代码语言:javascript
运行
复制
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就是本文的灵感来源。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50961802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档