How to "properly" create custom cell view for WinRT

Hi all.
For the life of me I cannot make this work. I need to create custom cells in listview with complex interiors (ie. not just a textcell). On android everything works flawless and I was looking forward to see it all run on WinRT but I hit a brick wall instead :/
Problem is the fundamental OnBindingContextChanged method does not seem to get called either for cell of cell view. I even tried to create plain old bindings but that didn't help either. There's also some strange behavior on WinRT like if I don't override the OnMeasure method the app crashes inside xamarin code, and the OnAppearing method of a page inside a tab page does not get called but those are details. Anyway problem should be apparent from this example:

using Xamarin.Forms;

namespace App1
{
    public sealed class MyList : ListView
    {
        public MyList()
            : base(ListViewCachingStrategy.RecycleElement)
        {
            ItemTemplate = null;
            IsGroupingEnabled = false;
            HasUnevenRows = false;
            RowHeight = 100;
            ItemsSource = new[] {"First", "Second"};
        }

        protected override Cell CreateDefault(object item) => new MyCell {View = new MyCellView()};
    }

    public sealed class MyCell : ViewCell
    {
        protected override void OnBindingContextChanged()
        {
            //bug: this never gets called :/

            base.OnBindingContextChanged();
            View.BindingContext = BindingContext;
        }
    }

    public sealed class MyCellView : Layout<View>
    {
        private readonly Label _lbl;

        public MyCellView()
        {
            BackgroundColor = Color.Gray.MultiplyAlpha(0.4);
            _lbl = new Label {Text = "Default text", VerticalTextAlignment = TextAlignment.Center, HorizontalTextAlignment = TextAlignment.Center};
            Children.Add(_lbl);
        }

        protected override void OnBindingContextChanged()
        {
            //bug: this never gets called either :(
            _lbl.Text = BindingContext?.ToString();
        }

        //bug: OnMeasure for some reason has to be overriden or theres unhandled NullReference exception in xam framework at runtime and it says (yes the message is even truncated):
//   System.NullReferenceException: Object reference not set to an instance of an object.
//   at Xamarin.Forms.Platform.WinRT.VisualElementRenderer2.GetDesiredSize(Double widthConstraint, Double heightConstraint)
//   at Xamarin.Forms.Platform.WinRT.Platform.Xamarin.Forms.IPlatform.GetNativeSize(VisualElement element, Double widthConstraint, Double heightConstraint)
//   at Xamarin.Forms.VisualElement.OnSizeRequest(Double widthConstraint, Double heightConstraint)
//   at Xamarin.Forms.VisualElement.OnMeasure(Doubl

        protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
            => new SizeRequest(new Size(widthConstraint, heightConstraint));

        protected override void LayoutChildren(double x, double y, double width, double height) => _lbl.Layout(new Rectangle(x, y, width, height));
    }

    public class App : Application
    {
        public App()
        {
            // The root page of your application
            MainPage = new ContentPage
            {
                Content = new MyList
                {
                    VerticalOptions = LayoutOptions.Fill,
                    HorizontalOptions = LayoutOptions.Fill
                }
            };
        }
    }
}

Steps to reproduce:
1. Create new xamarin multiplatform project with portable library
2. Copy&paste above code in the App.cs in the PCL
3. Try to run WinRT project on local machine or whatever
4. Expected result is a list view with two rows, first says "First" and second says "Second" (strings from data source) but that is not the case

What am I doing wrong here?

Best Answer

Answers

  • JohnHardmanJohnHardman GBUniversity mod
    edited August 2016

    @RudolfKurka - If you haven't already, can you log any previously unreported bugs that you have found at https://bugzilla.xamarin.com/

    Whilst there's no guarantee of them being fixed (or even triaged) quickly, there's more chance of something happening about them if they are in bugzilla ;-)

Sign In or Register to comment.