首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有自定义类型的UIButton丢失单击事件

具有自定义类型的UIButton丢失单击事件
EN

Stack Overflow用户
提问于 2016-09-09 00:40:54
回答 1查看 383关注 0票数 0

我有以下用Xamarin创建自定义iOS UIButton的代码:

代码语言:javascript
复制
    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按钮上没有收到任何单击的事件。

如何恢复丢失的点击功能?

EN

回答 1

Stack Overflow用户

发布于 2016-09-09 17:22:53

最后,在Xamarin源代码中,我发现了这个:

https://github.com/xamarin/Xamarin.Forms/blob/2d9288eee6e6f197364a64308183725e7bd561f9/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs

看看OnElementChanged,我有点惊讶,只有当OnElementChanged的这个实例创建原生元素时,才会附加事件。不确定为什么要这样做,但至少现在我知道,如果我自己创建一个控件,那么我将不得不自己连接click事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39396251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档