gpt4 book ai didi

ios - 自定义类型的 UIButton 丢失 Click 事件

转载 作者:行者123 更新时间:2023-11-29 00:41:24 25 4
gpt4 key购买 nike

我有以下代码在 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 按钮上没有收到任何 Clicked 事件。

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

最佳答案

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

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

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

关于ios - 自定义类型的 UIButton 丢失 Click 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39396251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com