[英]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. 我正在尝试在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();
}
The exception log has message after the exception occurred about a label and bindableobjects. 有关标签和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 还有一点,第一个块中的try / catch语句没有启动控制台
As always, any help is greatly appreciated 一如既往,非常感谢任何帮助
Here is the corrected code for adding an image to a ListView. 以下是将图像添加到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. 然后我将imageLayout添加到最终的StackLayout。 Works perfectly.
完美的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.