Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Subclassing ImageCell (why is View not exposed?)

$
0
0

I'm trying to determine the easiest way to implement a selectable ImageCell. I was hoping to do this:

public class SelectableImageCell : ImageCell
{
    const string SelectedImagePath = "icons/258-checkmark.png";
    public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(
        propertyName: "IsSelected",
        returnType: typeof(bool),
        declaringType: typeof(SelectableImageCell),
        defaultValue: false);

    public bool IsSelected 
    {
        get { return (bool)GetValue(IsSelectedProperty); }
        set { SetValue(IsSelectedProperty, value); }
    }

    Image _selectedImage = new Image { Source = FileImageSource.FromFile(SelectedImagePath), IsVisible = false};

    public SelectableImageCell ()
    {
        _selectedImage.SetBinding(Image.IsVisibleProperty, IsSelectedProperty.PropertyName);

        View = new ContentView 
        {
            Content = new AbsoluteLayout 
            {
                Children = 
                {
            // fails here -- base.View isn't available 
                    {base.View, new Rectangle (0, 0, 1, 1), AbsoluteLayoutFlags.All},
                    {_selectedImage, new Rectangle (0.97f, 0.5f, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize), AbsoluteLayoutFlags.PositionProportional},
                }
            }
        };
    }

}

This way I would be able to still bind to the ImageCell properties, and bind IsSelected to my model items. It seems kind of silly to have to write my own ImageCell by inheriting from ViewCell, just so I can add one more property to it. I imagine there's a good reason why there is no View exposed on ImageCell and the other cell types so I'm curious what it is.

Or perhaps I'm missing something and something close to my approach is possible?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>