c# - 膨胀通用/模板化自定义 View

标签 c# android xamarin.android xamarin

是否可以扩充模板化的自定义/子类 View ?

也就是说,下面的可以膨胀吗:

public class TextViewT<T> : TextView
{
    List<T> _list;

    public TextViewT(Context context) : base(context) { }
    public TextViewT(Context context, IAttributeSet attributes) : base(context, attributes) { }
}

如果是这样,这种类型在布局文件中的语法是什么?

具体来说,绝对类名 app.droid.views.custom.TextViewT 将如何出现?

<?xml version="1.0" encoding="utf-8"?>
<app.views.custom.TextViewT
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/text_view_t"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

尝试这样做时出现以下异常:

UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type 'App.Droid.Views.Custom.TextViewT, App.Droid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Type.GetType (string,bool) <0x00027>
at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <0x00057>
at (wrapper native-to-managed) Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <0x0006f>
at (wrapper delegate-invoke) <Module>.invoke_void__this___intptr_intptr_intptr_intptr_JValue[] (intptr,intptr,intptr,intptr,Android.Runtime.JValue[]) <0x000cb>
at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x00067>
at Android.App.Activity.SetContentView (int) <0x001d7>
at App.Droid.Views.CalendarView.Render () <0x00073>
at MonoCross.Droid.MXActivityView`1<App.ViewModels.CalendarVM>.OnCreate (Android.OS.Bundle) <0x00243>
at App.Droid.Views.CalendarView.OnCreate (Android.OS.Bundle) <0x0001b>
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x00057>
at (wrapper dynamic-method) object.cb51fcb8-c099-4fce-b266-642090f4d778 (intptr,intptr,intptr) <0x00043>

谢谢。

最佳答案

好吧,既然它在原始 android 中是不可能的,那么它在 Xamarin.Android 中肯定是不可能的:

Adding a custom view to XML... but with a GENERIC-type

关于c# - 膨胀通用/模板化自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18832084/

相关文章:

c# - 两个不同的用户可以访问同一个数据库表以插入不同的数据吗?

c# - 我如何从 ASP.NET 站点访问智能卡?

c# - 如何在 C# 函数中为 List<> 参数指定默认值?

android - 主题继承在 Android 中究竟是如何工作的?

android - 如何在表格布局的底部显示按钮?

Android - 在手机上授权计算机

c# - 在 C# 中返回 "Functional Interface"

xamarin - 引用netstandard库时,项目中不存在目标 "GetBuiltProjectOutputRecursive"

android - 带换行符的多行 TextView

android - 将 AChartEngine 1.0.0 绑定(bind)到 Mono For Android 项目不起作用