是什么导致我的Grid自定义渲染器让Xamarin.Android抛出此异常?

时间:2018-01-01 23:24:18

标签: xamarin.forms xamarin.android

例外是:

System.MissingMethodException
Constructor on type 'MyApp.Droid.MyGridRenderer' not found

自定义渲染器:

using System;
using Android.Content;
using Android.Graphics;
using Android.Text;
using Android.Util;
using MyApp;
using MyApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Grid), typeof(MyGridRenderer))]
namespace MyApp.Droid
{
    public class MyGridRenderer : ViewRenderer<Grid, Android.Views.View>
    {
        protected MyGridRenderer(Context context) : base(context)
        {
        }
    }
}

我有适用于Mac版本7.3.2的Visual Studio社区

以防万一,这是iOS的自定义渲染器,无需任何构造函数即可正常工作:

using System;
using CoreGraphics;
using CoreText;
using Foundation;
using MyApp;
using MyApp.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Grid), typeof(MyGridRenderer))]
namespace MyApp.iOS
{
    public class MyGridRenderer : ViewRenderer<Grid, UIView>
    {
    }
}

1 个答案:

答案 0 :(得分:5)

Android自定义渲染器的构造函数的签名在Xamarin表单的最新版本(2.5.0)中发生了更改,要求传递上下文。

You can see that here in the release notes.

这不是iOS所需要的。

尝试将protected更改为public。