我有以下用Xamarin创建自定义iOS UIButton的代码:
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
// will have to create a native Custom type of ios button to avoid text dimming
// when touched
// logic of element processing from
// https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/view/
// except we don't call the base immediately
// because it will create the Control for us, but we don't want that here
if (Control == null)
{
// instantiate the native control and assign it to the Control property with
// the SetNativeControl method
uiBtn = new UIButton(UIButtonType.Custom);
SetNativeControl(uiBtn); // should set Control to the newly created element
// and prevent base.OnElementChanged(e) from creating a new element instead
}
base.OnElementChanged(e);
// some button customization code here - it does not matter, the issue happens also without it...渲染器代码运行良好,但现在我在Xamarin.Forms按钮上没有收到任何单击的事件。
如何恢复丢失的点击功能?
发布于 2016-09-09 17:22:53
最后,在Xamarin源代码中,我发现了这个:
看看OnElementChanged,我有点惊讶,只有当OnElementChanged的这个实例创建原生元素时,才会附加事件。不确定为什么要这样做,但至少现在我知道,如果我自己创建一个控件,那么我将不得不自己连接click事件。
https://stackoverflow.com/questions/39396251
复制相似问题