获取 Xamarin Forms 自定义呈现复选框的值 [英] Get value of Xamarin Forms Custom Rendered Checkbox

查看:40
本文介绍了获取 Xamarin Forms 自定义呈现复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xamarin Forms 中正确呈现了一个复选框视图:

I have a Checkbox view rendering properly in Xamarin Forms:

public class CheckBoxRenderer : ViewRenderer<LegalCheckbox, CheckBox>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<LegalCheckbox> e)
    {
        base.OnElementChanged (e);
        CheckBox control = new Android.Widget.CheckBox(this.Context);
        control.Checked = false;
        control.Text = "I agree to terms";
        control.SetTextColor (Android.Graphics.Color.Rgb (60, 60, 60));
        this.SetNativeControl(control);
    }
}

我想检查这个复选框是否被选中.有没有办法做到这一点?我需要使用依赖服务吗?

I want to check whether or not this checkbox is checked or not. Is there a way I can do this? Do I need to use dependency service?

推荐答案

不要认为依赖服务会在这里提供帮助,它只是一个 IOC 容器.面对同样的问题,我可能只是设置了一些事件

Dont think Dependency Services is going to help here its just an IOC container. Faced with the same issue I'd likely just set up some eventing

public class CheckBoxRenderer : ViewRenderer<LegalCheckbox, CheckBox>
{
    private LegalCheckbox legalCheckBox;
    protected override void OnElementChanged (ElementChangedEventArgs<LegalCheckbox> e)
    {
        base.OnElementChanged (e);
        legalCheckBox = e.NewElement;
        CheckBox control = new Android.Widget.CheckBox(this.Context);
        control.Checked = false;
        control.Text = "I agree to terms";
        control.SetTextColor (Android.Graphics.Color.Rgb (60, 60, 60));
        base.SetNativeControl(control);
        Control.Click+=(sender,evt)=> 
            legalCheckBox.Checked = ((CheckBox)sender).Checked;
    }
}

同样在我的视图中添加一个道具

likewise adding a prop to my View

public class LegalCheckbox : View
{
    public bool Checked { 
        get; 
        set; 
    }
    public LegalCheckbox ()
    {
    }
}

为什么?我倾向于将平台控件和 Xamarin 控件之间的关系视为模型-视图-适配器模式.平台控件是您的视图.您的 Xamarin 控件是您的模型,而您的渲染器则在两者之间进行改编.为了实现这一点,我们需要在 LegalCheckbox(Model) 的某个地方存储我们想要收集的数据.然后我们必须将它连接起来,以便对视图的更改导致模型的更改.因此,我们为 View 中的 OnClick 事件添加 EventHandler 以更新我们的模型.

Why? I tend to think of the relationship between the Platform controls and the Xamarin Controls as a Model-View-Adapter pattern. The Platform Controls are your View. Your Xamarin Controls are your Model and your renderer does the Adaptations between. To accomplish this we need somewhere in LegalCheckbox(Model) to store the Data we want to collect. Then we have to wire it up so that changes to the View cause changes to the model. Thus we add in the EventHandler for the OnClick event in the View to update our model.

乍一看,所有这些都发生在 OnelementChanged 事件期间.然而,它只是在那时实际连接,直到 OnElementChanged 完成并且其资源被销毁后,实际的 Checked 事件才会触发.因此,我们需要在 OnElementChanged 事件处理程序的外部引用我们的 LegalCheckbox,当 Clicked 事件触发时,该处理程序仍然存在.

It might appear at initial glace that all of this happens during the OnelementChanged Event. However its only actually wired up during at that point the actual Checked Event wont fire till long after OnElementChanged has completed and its resources been destroyed. Thus we need a reference to our LegalCheckbox out side of the OnElementChanged Event Handler that will still be there when the Clicked event fires.

这篇关于获取 Xamarin Forms 自定义呈现复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆