Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I simplify this iOS renderer now that I no longer need to explicitly subscribe to property-changed-event

I am using this custom renderer:

public class ExtViewCellRenderer : ViewCellRenderer
{
    UITableViewCell _nativeCell;

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        _nativeCell = base.GetCell(item, reusableCell, tv);
        var formsCell = item as ExtViewCell;

        if (formsCell != null)
        {
            formsCell.PropertyChanged -= OnPropertyChanged;
            formsCell.PropertyChanged += OnPropertyChanged;
        }
        SetTap(formsCell);

        return _nativeCell;
    }

    void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var formsCell = sender as ExtViewCell;
        if (formsCell == null)
            return;

        if (e.PropertyName == ExtViewCell.NoTapProperty.PropertyName)
        {
            SetTap(formsCell);
        }
    }

    private void SetTap(ExtViewCell formsCell)
    {
        if (formsCell.NoTap)
            _nativeCell.SelectionStyle = UITableViewCellSelectionStyle.None;
        else
            _nativeCell.SelectionStyle = UITableViewCellSelectionStyle.Default;
    }

}

I read that with a TextCellRenderer it's no longer necessary to explicitly subscribe to property-changed-event as there is a base overridable method HandlePropertyChanged that can be re-used in this context.

Can someone tell me if this is the case also for the ViewCellRenderer and if so then how could I modify this code to make use of this?

I also saw code like this in another renderer:

public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
    var textCell = (TextCell)item;
    var fullName = item.GetType().FullName;
    cell = tv.DequeueReusableCell(fullName) as CellTableViewCell;

    //...

But not here. Is it necessary to do this Cell = tv.DequeueReusableCell ?

like image 506
Alan2 Avatar asked Oct 28 '22 23:10

Alan2


1 Answers

I read that with a TextCellRenderer it's no longer necessary to explicitly subscribe to property-changed-event as there is a base overridable method HandlePropertyChanged that can be re-used in this context.

Yes that is correct according to its source code

protected virtual void HandlePropertyChanged(object sender, PropertyChangedEventArgs args) {
    //...
}

TextCellRenderer Source

Can someone tell me if this is the case also for the ViewCellRenderer and if so then how could I modify this code to make use of this?

No it is not the case also for the ViewCellRenderer as according to source code ViewCellRenderer is not exposing its ViewCellPropertyChanged as it is private

void ViewCellPropertyChanged(object sender, PropertyChangedEventArgs e) {
    //...
}

ViewCellRenderer Source

You could always modify it yourself and create a pull request to add that feature.

like image 58
Nkosi Avatar answered Nov 15 '22 06:11

Nkosi