How to avoid TargetInvocationException when calling constructor of custom view which loaded dynamically by Xamarin.Android Application comiled by VS 2019/Xamarin.

啓一 木暮 1 Reputation point

On Visual Studio 2017/Xamarin.Android, it is possible to create custom view in class library assembly by executing Activator.Activator.CreateInstance method. But On Visual Studio 2019/Xamarin.Android, TargetInvocationException (no java peer type found) are thrown.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotSupportedException: Cannot create instance of type 'ClassLibrary.ButtonEx': no Java peer type found.
at Java.Interop.JniPeerMembers+JniInstanceMethods..ctor (System.Type declaringType)
at Java.Interop.JniPeerMembers+JniInstanceMethods.GetConstructorsForType (System.Type declaringType)
at Java.Interop.JniPeerMembers+JniInstanceMethods.StartCreateInstance (System.String constructorSignature, System.Type declaringType, Java.Interop.JniArgumentValue* parameters)
at Android.Widget.Button..ctor (Android.Content.Context context)
at ClassLibrary.ButtonEx..ctor (Android.Content.Context context)

Details: 1655173

So, I need help to avoid TargetInvocationException in this case.

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,206 questions
{count} votes