为什么我的自定义ButtonRenderer不起作用? [英] Why is my custom ButtonRenderer not working?

查看:178
本文介绍了为什么我的自定义ButtonRenderer不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Xamarin.Forms创建一个自定义的ButtonRenderer.这是我一直在尝试按照一些教程进行的一个简单测试,但是似乎可以使它正常工作.

I'm trying to create a custom ButtonRenderer for Xamarin.Forms. Here is a simple test I've been trying to put up together following some tutorials, but I can's seem to make it work.

这是我的.xaml页面:

Here is my .xaml page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestProject.MainPage">
    <ContentPage.Content>
        <Button VerticalOptions="Center" HorizontalOptions="Center"></Button>
    </ContentPage.Content>
</ContentPage>

这是我的自定义呈现:(放置在Android项目中)

And here is my custom rendered: (it is placed in the Android project)

[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(CustomButtonRenderer))]
namespace TestProject.Droid.CustomRenderers
{
    public class CustomButtonRenderer: ButtonRenderer
    {
        public CustomButtonRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            Control.SetBackgroundColor(Android.Graphics.Color.Red);
        }
    }
}

但是它从未被调用,我的应用程序崩溃了.我的日志显示:

But it never gets called and my app crashes. My logcat shows:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Java.Lang.ClassNotFoundException: Didn't find class "md5dba8ede99752acada1f5ba384c7cf839.CustomButtonRenderer" on path: DexPathList[[zip file "/data/app/com.companyname.TestProject-1/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.GN.Mobile.TestProject-1/lib/arm, /data/app/com.companyname.GN.Mobile.TestProject-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]

我在这里想念东西吗?

推荐答案

我仍在对此进行调查.您的班级缩减了,因为它不是在PCL中静态链接的.通过为您的类命名,可以避免这种情况:

I'm still investigating on this. Your class gets shrunk because it's not statically linked in your PCL. You avoid that by giving a name to your classes like this:

[Activity(Name = "somepackage.custombuttonrenderer")]
public class CustomButtonRenderer: ButtonRenderer
{ }

如果您能够将最低Android版本定位为Android 5.0(Api 21),则该问题应会消失,因为使用了另一个版本的Dex文件.

If you're able to target the minimum Android version to Android 5.0 (Api 21) this problem should disappear as another version of the Dex file is used.

这篇关于为什么我的自定义ButtonRenderer不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆