Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MissingMethodException for custom renderer

When loading my MainPage Visual Studio goes into break mode with the following error

System.MissingMethodException: Default constructor not found for type Test.Renderers.PostListViewAndroid

I updated my Custom Renderer class to support 2.5 since Context is obsolete as of version 2.5

My custom renderer is

[assembly: ExportRenderer(typeof(PostListView), typeof(PostListViewAndroid))]
namespace SocialNetwork.Droid.Renderers
 {
public class PostListViewAndroid: ListViewRenderer
{

    public PostListViewAndroid(Context context) : base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
    {
        base.OnElementChanged(e);
        Control.SetSelector(Android.Resource.Color.Transparent);
    }
}
}

And the PostListView is simply

public class PostListView : ListView
{
}
like image 732
Dan Avatar asked Jan 03 '23 19:01

Dan


2 Answers

If you are using Xamarin.Forms >= 2.5 you need to use ListViewRenderer(Context context) : base(context) as you know

This is the source for the current ListViewRenderer

public class ListViewRenderer : ViewRenderer<ListView, AListView>, SwipeRefreshLayout.IOnRefreshListener
{
    ...

    public ListViewRenderer(Context context) : base(context)
    {
        AutoPackage = false;
    }

    [Obsolete("This constructor is obsolete as of version 2.5. Please use ListViewRenderer(Context) instead.")]
    public ListViewRenderer()
    {
        AutoPackage = false;
    }

Notice the Obsolete Attribute, meaning you don't need it. However it seems your problem is that your packages and references are out of alignment. Id try the following steps

Step 1

  1. Clean Solution
  2. Delete bins and objs directions in all projects
  3. Restart visual studio
  4. Rebuild

Step 2

Try updating all packages for solution in the package package console

Update-Package  –reinstall

After this step, if it still hasn't worked try step 1 again

like image 97
TheGeneral Avatar answered Jan 05 '23 17:01

TheGeneral


If base class does not have default empty constructor (in your case ListViewRenderer) - you cannot do this. If it has, then add below

public PostListViewAndroid()
    {

    }

Its for simple reason : when derive class in instantiated, first base class constructor is invoked for initialization. So if there is no default constructor in base class, its not going to work out.

like image 45
rahulaga_dev Avatar answered Jan 05 '23 18:01

rahulaga_dev