Xamarin.Android Creating And Using Custom Controls

青春壹個敷衍的年華 提交于 2019-12-13 03:01:56

问题


Another tough day with Xamarin!

So, today, i am trying to figure out how one can create and use a custom control in Xamarin.Android. I read all the docs and all were on Xamarin.Forms. I wonder why there is so less support for Xamarin.Android.

Anyways, i came across a few questions on SO and tried this :

TestControl.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)
    {
    }
}
}

TestControl.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">

In my activity_main.axml file, i have the following:

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

Even though intellisense is able to find TestApp.CustomControls.Classes.TestControl but i don't see anything on the screen. Maybe because i never linked the xml file with the class?(Still now sure how xamarin.android works).

So, what should be my steps to get the custom control working/visible on view?

来源:https://stackoverflow.com/questions/55564952/xamarin-android-creating-and-using-custom-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!