2017-06-20 206 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。完美的作品。