Custom ListView - TypeInitializationException exception

RianDutraRianDutra ✭✭USMember ✭✭
edited March 2017 in Xamarin.Forms

I'm trying to create a Custom ListView which has a binding property (bool).

I followed this tutorial (part Boolean Flip):
https://xamarinhelp.com/access-a-control-method-from-a-viewmodel/

When I run the app and access the page which has this custom ListView, it crashes with this exception:

Exception occurred while: Calling constructor RianProject.Views.MyPage().
Exception is: TypeInitializationException - The type initializer for 'RianProject.Controls.CustomListView' threw an exception.

Here is my custom ListView:

using System.Diagnostics;
using Xamarin.Forms;

namespace RianProject.Controls {

    public class CustomListView : ListView {

        public bool HasToDo {
            get { return (bool)GetValue(HasToDoProperty); }
            set { SetValue(HasToDoProperty, value); }
        }

        public static BindableProperty HasToDoProperty =
            BindableProperty.Create(
               nameof(HasToDo),
               typeof(bool),
               typeof(CustomListView), // I also tried typeof(ListView)
               propertyChanged: (bindable, oldValue, newValue) => {

                  Debug.WriteLine("Something happens!");
            }
        );

        public ConversationListView() {
        }
    }
}

XAML:

<ContentPage ...
             xmlns:Controls="clr-namespace:RianProject.Controls;assembly=RianProject">

<Controls:CustomListView
                HasToDo="{Binding HasToDo}"
                ... />
...

And the ViewModel has the HasToDo binding property (bool).

Posts

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭
    edited March 2017

    @RianDutra
    I copied your class... And put a CustomListView on a page of an existing project.
    It didn't fail. Which makes me think you've got something wrong elsewhere.

    I'm also wondering about your need/approach here. Why would a ListView have any kind of ToDo property on it? I can see where one of the items in the collection that is the binded source might have some sort of ToDo on the item itself. But on a ListView control? Seems odd. But there is little to go on so I have no way to know what your long-term end goal for this control is.

Sign In or Register to comment.