Default constructor not found exception?

jtthjtth Member ✭✭
edited March 2018 in Xamarin.iOS

Below we have the following: a basic content page (MyPage), a class extending BaseViewModel (CustomClass), and a class extending ViewCell (CustomCells). The page has a listView which uses CustomClass as the itemSource (named listViewSource, see variable declarations) and CustomCells as the itemTemplate. This set-up was working without a hitch; no changes were made (to my recollection) and out of the blue, Visual Studio started generating this:

System.MissingMethodException: Default constructor not found for type CustomCells

As one can see, CustomCells does in fact contain a constructor. No idea where to go from here.

Full example:

using System;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace MyApp
{
    public class MyPage : ContentPage
    {
        // Variables
        public ObservableCollection<CustomClass> listViewSource { get; set; }

        public MyPage()
        {
            // Create listView
            ListView myListView = new ListView
            {
                RowHeight = 40,
                SeparatorVisibility = SeparatorVisibility.None,
                ItemTemplate = new DataTemplate(typeof(CustomCells)),
                ItemsSource = listViewSource,
            };

            // Prevent highlight via selection
            myListView.ItemSelected += (sender, e) =>
            {
                ((ListView)sender).SelectedItem = null;
            };

            // Page layout
            var ListViewLayout = new StackLayout
            {
                Padding = new Thickness(0, 20, 0, 0),
            };

            // Add children to layout
            ListViewLayout.Children.Add(myListView);

            // Assign content
            Content = ListViewLayout;
        }
    }

    public class CustomClass : BaseViewModel
    {
        public int LabelNumber { get; set; }
    }

    public class CustomCells : ViewCell
    {
        public CustomCells()
        {
            //
            // Instantiate views
            //
            var labelVar = new Label
            {
                LineBreakMode = LineBreakMode.NoWrap,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center,
                TextColor = Color.White,
                FontSize = 10,
                WidthRequest = 40,
            };

            //
            //  Set bindings
            //
            labelVar.SetBinding(Label.TextProperty, new Binding("LabelNumber"));

            //
            //  Instantiate layouts
            //
            var horizontalLayout = new StackLayout()
            {
                BackgroundColor = Color.Black,
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.Fill,
                VerticalOptions = LayoutOptions.Center,
            };

            //
            //  Add views to the view hierarchy
            //
            horizontalLayout.Children.Add(labelVar);

            //
            //  Create swipe gesture actions
            //
            var moreAction = new MenuItem { Text = "More" };
            moreAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
            moreAction.Clicked += async (sender, e) =>
            {
                var mi = ((MenuItem)sender);
                Console.WriteLine("More Context Action clicked: " + mi.CommandParameter);
            };

            //
            //  Add to the ViewCell's ContextActions property
            //
            ContextActions.Add(moreAction);

            //
            //  Add to parent view
            //
            View = horizontalLayout;
        }
    }
}
Tagged:

Best Answer

  • jtthjtth ✭✭
    edited October 2018 Accepted Answer

    @15mgm15 said:
    @jtth did you found a solution for this?

    Solution was my Visual Studio installation was all kinds of jacked up. Followed this to wipe the installation. Re-installed fresh and this issue disappeared.

Answers

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @jtth did you found a solution for this?

  • AdrianGhiAdrianGhi USMember ✭✭

    Try to add an empty constructor like:

    public CustomCells(){}

    I know it sounds stupid, because you have already a constructor, but I had this problem too in Xamarin Android and it worked with an empty constructor.

  • jtthjtth Member ✭✭
    edited October 2018 Accepted Answer

    @15mgm15 said:
    @jtth did you found a solution for this?

    Solution was my Visual Studio installation was all kinds of jacked up. Followed this to wipe the installation. Re-installed fresh and this issue disappeared.

Sign In or Register to comment.