首页 > 解决方案 > Xamarin.Android 创建和使用自定义控件

问题描述

Xamarin 又是艰难的一天!

所以,今天,我想弄清楚如何在Xamarin.Android. 我阅读了所有文档,并且都在Xamarin.Forms. 我想知道为什么对Xamarin.Android.

无论如何,我遇到了一些关于 SO 的问题并尝试了这个:

测试控制.cs

namespace TestApp.CustomControls.Classes
{
public class TestControl : ViewGroup
{
    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        throw new NotImplementedException();
    }
    public TestControl(Context context, IAttributeSet attrs)
        : base(context, attrs)
    {
    }
}
}

测试控制.xml

<TestApp.CustomControls.Classes.TestControl xmlns:android="http://schemas.android.com/apk/res/android"
                                                 android:layout_height="20dp" android:layout_width="20dp"
                                                 android:background="#fff">

在我的activity_main.axml文件中,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_height="50dp"
android:layout_weight="50dp">

<TestApp.CustomControls.Classes.TestControl android:layout_height="50dp"
android:layout_weight="50dp"/>
</RelativeLayout>

即使智能感知能够找到TestApp.CustomControls.Classes.TestControl,但我在屏幕上看不到任何东西。也许是因为我从未将 xml 文件与类链接?(现在仍然确定 xamarin.android 是如何工作的)。

那么,让自定义控件工作/可见的步骤应该是什么?

标签: c#androidxamarinxamarin.android

解决方案


推荐阅读