简体   繁体   中英

LoadApplication causing program to crash

I am trying to work an example in Xamarin Mobile App Dev, and keep getting an invalid cast exception on LoadApplication(new ListViewExample.app) call.

        global::Xamarin.Forms.Forms.Init(this, bundle);
        try
        {
            LoadApplication(new ListViewExample.App());
        }
        catch (InvalidCastException ice)
        {
            Console.Write(ice.InnerException);
            Console.Write(ice.Message);
            Console.Read();
        }
        catch (Exception e)
        {
            Console.Write(e.InnerException);
            Console.Write(e.Message);
            Console.Read();
        }

The exception log has message after the exception occurred about a label and bindableobjects.

Here is the code that is being run

    using System;
    using System.Collections.Generic;
    using System.Text;

    using Android.App;
    using Android.Content.PM;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;

    using Xamarin.Forms;

    namespace ListViewExample
    {
        public class ListItem
        {
            public string Source { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
            public string Price { get; set; }
        }

        public class ListViewCustom : ContentPage
        {
            public ListViewCustom()
            {
                ListView listView = new ListView();
                listView.ItemsSource = new ListItem[]
                {
                    new ListItem{Source="first.png",Title="First",Description="1st item", Price="$100.00" },
                    new ListItem{Source="second.png",Title="Second",Description="2nd item", Price="$200.00" },
                    new ListItem{Source="third.png",Title="Third",Description="3rd item", Price="$300.00" }
               };

                listView.RowHeight = 80;
                listView.BackgroundColor = Color.Black;
                listView.ItemTemplate = new DataTemplate(typeof(ListItemCell));
                Content = listView;

                listView.ItemTapped += async (sender, e) =>
                {
                    ListItem item = (ListItem)e.Item;
                    await DisplayAlert("Tapped", item.Title.ToString(), "         was selected.", "OK");
                    ((ListView)sender).SelectedItem = null;
                };
            }
        }

        public class ListItemCell : ViewCell
        {
            public ListItemCell()
            {
                Image image = new Image()
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                image.SetBinding(Label.TextProperty, "Source");

                StackLayout imageLayout = new StackLayout()
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Children =
                    {image}
                };

                Label titleLabel = new Label()
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    FontSize = 25,
                    WidthRequest = 100,
                    FontAttributes = Xamarin.Forms.FontAttributes.Bold,
                    TextColor = Color.White
                };
                titleLabel.SetBinding(Label.TextProperty, "Title");

                Label descLabel = new Label()
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    FontSize = 12,
                    WidthRequest = 100,
                    TextColor = Color.White
                };
                descLabel.SetBinding(Label.TextProperty, "Description");

                StackLayout viewLayoutItem = new StackLayout()
                {
                    HorizontalOptions = LayoutOptions.StartAndExpand,
                    Orientation = StackOrientation.Vertical,
                    Padding = new Thickness(10, 0, 50, 10),
                    Children =
                    {
                        titleLabel,
                        descLabel
                    }
                };

                Label priceLabel = new Label()
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    FontSize = 25,
                    FontAttributes = Xamarin.Forms.FontAttributes.Bold,
                    TextColor = Color.Aqua
                };
                priceLabel.SetBinding(Label.TextProperty, "Price");

                StackLayout viewLayout = new StackLayout()
                {
                    HorizontalOptions = LayoutOptions.StartAndExpand,
                    Orientation = StackOrientation.Horizontal,
                    Padding = new Thickness(25, 10, 55, 15),
                    Children =
                    {
                        imageLayout,
                        viewLayoutItem,
                        priceLabel
                    }
                };

                View = viewLayout;
            }
        }
    }

Something else, the try/catch statements in the first block don't bring up the console

As always, any help is greatly appreciated

Here is the corrected code for adding an image to a ListView.

    Image image = new Image()
        {
            HorizontalOptions = LayoutOptions.Start,
            WidthRequest = 40
        };
        image.SetBinding(Image.SourceProperty, "Source");

        StackLayout imageLayout = new StackLayout()
        {
            Orientation = StackOrientation.Vertical,
            Children = { image }
        };

Then I added the imageLayout to the final StackLayout. Works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM