如何创建 Xamarin.Forms 自定义渲染器,其中渲染器基础 class 使用泛型?
How to create a Xamarin.Forms custom renderer, where the renderer base class uses generics?
我有一个基地class,它看起来像下面
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
}
}
问题是
The type or namespace name 'Tobject' could not be found (are you missing a using directive or an assembly reference?)
我可以改用 object
,但是自定义渲染器永远不会被调用。
是否有获取正确类型或使用泛型的选项?我应该如何定义 ExportRenderer
?
好的解决方案已发布here。
基础class:
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
}
}
我有一个基地class,它看起来像下面
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
}
}
问题是
The type or namespace name 'Tobject' could not be found (are you missing a using directive or an assembly reference?)
我可以改用 object
,但是自定义渲染器永远不会被调用。
是否有获取正确类型或使用泛型的选项?我应该如何定义 ExportRenderer
?
好的解决方案已发布here。
基础class:
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
}
}