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

查看:28
本文介绍了为什么我的自定义 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);
        }
    }
}

但它永远不会被调用,我的应用程序崩溃了.我的 logcat 显示:

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]]

我在这里遗漏了什么吗?

Am I missing something here?

推荐答案

我仍在对此进行调查.您的班级缩小了,因为它没有在您的 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天全站免登陆