LoadApplication导致程序崩溃

时间:2017-06-20 23:39:38

标签: c# android xamarin

我正在尝试在Xamarin Mobile App Dev中运行一个示例,并继续在LoadApplication(new ListViewExample.app)调用上获得无效的强制转换异常。

        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();
        }

异常日志在标签和bindableobjects发生异常后发出消息。

以下是正在运行的代码

    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;
            }
        }
    }

其他方面,第一个区块中的try / catch语句不会显示控制台

与往常一样,非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

以下是将图像添加到ListView的更正代码。

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

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

然后我将imageLayout添加到最终的StackLayout。效果很好。