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

Building a ListView in code, where am I going wrong?

$
0
0

I've been having issues with my XAML crashing when scrolling a listview with a custom render inside. I'm going to try and take a stab at doing it in code, and I'm just wondering why I'm getting a crash.

Here's my code

    private StackLayout BuildListView(MediaPageViewModel viewModel)
    {
        var headerTemplate = new DataTemplate(() => new ModuleMediaListHeaderTemplate());
        headerTemplate.CreateContent();

        var itemTemplate = new DataTemplate(() => new ModuleMediaListItemTemplate());
        itemTemplate.CreateContent();

        _listView = new ListView
        {
            ItemsSource = viewModel.MediaSections,
            IsGroupingEnabled = true,
            GroupDisplayBinding = new Binding("SectionName"),
            HasUnevenRows = true,
            GroupHeaderTemplate = headerTemplate,
            ItemTemplate = itemTemplate
        };
        _listView.ItemSelected += (sender, args) => TriggerMedia((MediaViewModel)args.SelectedItem);

        var stackLayout = new StackLayout();
        stackLayout.Children.Add(_listView);

        return stackLayout;
    }

public class ModuleMediaListHeaderTemplate : ContentPage
{
    public ModuleMediaListHeaderTemplate()
    {
        var title = new Label();
        title.SetBinding(Label.TextProperty, new Binding("SectionName", BindingMode.Default, new UppercaseValueConverter()));

        var stackLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { title } };
        Content = stackLayout;
    }
}

public class ModuleMediaListItemTemplate : ContentPage
{
    public ModuleMediaListItemTemplate()
    {
        var title = new Label();
        title.SetBinding(Label.TextProperty, new Binding("Title", BindingMode.Default, new UppercaseValueConverter()));

        var stackLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { title } };
        Content = stackLayout;
    }
}

But when I run this up, I get this exception.

07-14 12:13:50.959 E/mono (18229): Unhandled Exception:
07-14 12:13:50.959 E/mono (18229): System.InvalidCastException: Cannot cast from source type to destination type.
07-14 12:13:50.959 E/mono (18229): at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
07-14 12:13:50.959 E/mono (18229): at Xamarin.Forms.TemplatedItemsList2[Xamarin.Forms.ItemsView1[Xamarin.Forms.Cell],Xamarin.Forms.Cell].InsertGrouped (System.Object item, Int32 index) [0x00000] in :0
07-14 12:13:50.959 E/mono (18229): at Xamarin.Forms.TemplatedItemsList2[Xamarin.Forms.ItemsView1[Xamarin.Forms.Cell],Xamarin.Forms.Cell].GroupedReset () [0x00000] in :0
07-14 12:13:50.959 E/mono (18229): at Xamarin.Forms.TemplatedItemsList2[Xamarin.Forms.ItemsView1[Xamarin.Forms.Cell],Xamarin.Forms.Cell].OnCollectionChangedGrouped (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in :0
07-14 12:13:50.959 E/mono (18229): at Xamarin.Forms.TemplatedItemsList2[Xamarin.Forms.ItemsView1[Xamarin.Forms.Cell],Xamarin.Forms.Cell].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, Boolean fixWindows) [0x00000] in <filenam
The program 'Mono' has exited with code 0 (0x0).

What might I be doing wrong?


note: I tried this approach as well, without any success.

var itemTemplate = new DataTemplate(typeof(ModuleMediaListItemTemplate));

Viewing all articles
Browse latest Browse all 58056

Trending Articles



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