Xamarin.Forms 2.5.0 and Context

流过昼夜 提交于 2019-11-30 22:31:58

问题


Today I updated to Xamarin.Forms 2.5.0 and saw, that I get the following warnings:

  • From the Android sub-project:

    Warning CS0618 'Forms.Context' is obsolete: 'Context is obsolete as of version 2.5. Please use a local context instead.'

How can I get the local context instead of Forms.Context? Is the Android Context meant?

  • From the custom renderer:

    Warning CS0618 'ButtonRenderer.ButtonRenderer()' is obsolete: 'This constructor is obsolete as of version 2.5. Please use ButtonRenderer(Context) instead.'

In my ButtonRenderer I only have the OnElementChanged() method, so what should I change here? Simply add a ButtonRenderer(Context) constructor? I still get the warning, if I do this in my platform renderer class. Does anyone have an example? The official documentation doesn't mention it and Google also doesn't bring some useful results, except the open source code of ButtonRenderer. This change also concerns many other renderer classes.

Does anyone had experienced other changes, which brakes plugins and so on?

PS: Also I didn't found out, when Device.Windows was deprecated. Now I replaced it with Device.UWP.


回答1:


I had this same issue for a SearchBarRenderer and all I needed to do to fix it was add a constructor like so:

public ShowSearchBarRenderer(Context context) : base(context)
{
}

Hope that answers the second part of your question.




回答2:


There are two questions here:

  1. How do I update Custom Renderers to use a local context?
  2. How can I access the current context now that Xamarin.Forms.Forms.Context is obsolete?

How to Update Custom Renderers

Add the overloaded Constructor to each custom renderer

Here is an example using a ButtonRenderer

[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace MyApp.Droid
{
    public class CustomButtonRenderer : ButtonRenderer
    {
        public CustomButtonRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            //ToDo: Customize Button
        }
    }
}

How to Access The Current Context

Install @Motz's CurrentActivityPlugin.

Now, you can call CrossCurrentActivity.Current.Activity when you need to access the current activity.

Here's an example of how to open the App's Settings in Xamarin.Forms.

[assembly: Dependency(typeof(DeepLinks_Android))]
namespace MyApp.Droid
{
    public class DeepLinks_Android : IDeepLinks
    {
        Context CurrentContext => CrossCurrentActivity.Current.Activity;

        public Task OpenSettings()
        {
            var myAppSettingsIntent = new Intent(Settings.ActionApplicationDetailsSettings, Android.Net.Uri.Parse("package:" + CurrentContext.PackageName));
            myAppSettingsIntent.AddCategory(Intent.CategoryDefault);

            return Task.Run(() =>
            {
                try
                {
                    CurrentContext.StartActivity(myAppSettingsIntent);
                }
                catch (Exception)
                {
                    Toast.MakeText(CurrentContext.ApplicationContext, "Unable to open Settings", ToastLength.Short);
                }
            });
        }
    }
}



回答3:


use Android.App.Application.Context

There is a discussion of this topic on the Forums



来源:https://stackoverflow.com/questions/47417015/xamarin-forms-2-5-0-and-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!