2017-06-20 204 views
1

我想在Xamarin移动应用程序开发中工作一个示例,并且在LoadApplication(新ListViewExample.app)调用中不断收到无效的转换异常。LoadApplication导致程序崩溃

 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语句不调出控制台

与往常一样,任何帮助是极大的赞赏

+0

开始注释代码,直到您运行代码,然后重新启用代码部分,直到找到引起中断的代码。我将开始使用内置的TextCell而不是自定义单元格。如果可行,请尝试使用仅有一个元素的简单ViewCell,然后逐个添加更多元素。 – Jason

+0

谢谢杰森,我会给它一个旋转 – user182162

+0

这是做的伎俩。我没有这个形象,但至少我能够继续前进。谢谢杰森 – user182162

回答

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。完美的作品。