在ListViewRenderer中获取绑定属性

时间:2018-04-10 05:24:03

标签: c# xamarin xamarin.forms xamarin.ios

我正在开发一个Xamarin.Forms项目,并添加了一个bool值来启用/禁用默认显示在ios中的额外单元格。

我的自定义渲染器是

 public class CustomListView : ListView
{
    public static readonly BindableProperty ShowExtraCellsProperty =
        BindableProperty.Create("ShowExtraCells", typeof(bool), typeof(CustomListView), true);

    public bool ShowExtraCells
    {
        get
        {
            return (bool)GetValue(ShowExtraCellsProperty);
        }
        set
        {
            SetValue(ShowExtraCellsProperty, value);
        }
    }
}

我的iOS渲染器是

public class CustomListViewiOS : ListViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {

            var listView = Control as UITableView;
            listView.SeparatorInset = UIEdgeInsets.Zero;
            this.Control.TableFooterView = new UIView();
        }
    }
}

我的问题是我找不到发件人将其强制转换为CustomListView以便能够获取该属性的值。

1 个答案:

答案 0 :(得分:1)

请从此处找到解决方案

    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {

            var listView = Control as UITableView;
            listView.SeparatorInset = UIEdgeInsets.Zero;
            this.Control.TableFooterView = new UIView();
        }
        var element = (your Interface name here / Control which you want to render)this.Element;
        //afterwards you can Access your Properties from **element** Like,
        element.ShowExtraCells = true;
    }