【问题标题】:How to create a Xamarin.Forms custom renderer, where the renderer base class uses generics?如何创建 Xamarin.Forms 自定义渲染器,其中渲染器基类使用泛型?
【发布时间】:2019-11-02 10:17:49
【问题描述】:

我有一个基类,如下所示

public class DialogSelectionItem<Tobject> : ViewCell

现在我想给它附加一个自定义渲染器:

[assembly: ExportRenderer(typeof(SomeApp.CustomRenderers.DialogSelectionItem<Tobject>), typeof(SomeApp.iOS.CustomRenderers.DialogSelectionItemRenderer))]
namespace SomeApp.iOS.CustomRenderers
{
    public class DialogSelectionItemRenderer : ViewCellRenderer
    {
        // some customizations
    }
}

问题是

找不到类型或命名空间名称“Tobject”(您是否缺少 using 指令或程序集引用?)

我可以改用object,但是自定义渲染器永远不会被调用。

是否可以选择获取正确的类型或使用泛型?我应该如何定义ExportRenderer

【问题讨论】:

  • 您如何在 XAML 中使用自定义 ViewCell?
  • 我刚刚测试了它,但使用了通用标签并使用了typeof(CustomLabel&lt;object&gt;),它被成功调用了。
  • @Vahid:我这样称呼它ItemTemplate = new DataTemplate(typeof(DialogSelectionItem&lt;Tobject&gt;))。就我而言,该应用程序似乎取决于类型。 object 不是我的 valid 类型。至少在我看来,这是对行为差异的解释。

标签: c# generics xamarin.forms custom-renderer


【解决方案1】:

好的,解决方案已发布here

基类:

public class DialogSelectionItem : ViewCell
{
    // nothing
}

public class DialogSelectionItem<Tobject> : DialogSelectionItem
{
    // do something
}

查看渲染器:

[assembly: ExportRenderer(typeof(SomeApp.CustomRenderers.DialogSelectionItem), typeof(SomeApp.iOS.CustomRenderers.DialogSelectionItemRenderer))]
namespace SomeApp.iOS.CustomRenderers
{
    public class DialogSelectionItemRenderer : ViewCellRenderer
    {
        // some customizations
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2014-08-29
    • 2017-01-07
    相关资源
    最近更新 更多