So I have this class:
public class MenuItem
{
public string image { private set; get; }
public string text { private set; get; }
public MenuItem (string image, string text)
{
this.image = image;
this.text = text;
}
}
And the following ViewCell class:
public class MenuItemCell : ViewCell
{
public MenuItemCell ()
{
Grid grid = new Grid
{
VerticalOptions = LayoutOptions.FillAndExpand,
RowDefinitions =
{
new RowDefinition { Height = new GridLength(100, GridUnitType.Auto) },
},
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(50, GridUnitType.Auto) },
new ColumnDefinition { Width = new GridLength(100, GridUnitType.Auto) },
}
};
var menuImage = new Image
{
IsVisible = true,
Aspect = Aspect.AspectFit,
};
var text = new Label {
TextColor = Color.Yellow,
BackgroundColor = Color.White,
};
menuImage.SetBinding (ImageCell.ImageSourceProperty, "image");
text.SetBinding (Label.TextProperty, "text");
grid.Children.Add (menuImage, 0, 1, 0, 1);
grid.Children.Add (text, 1, 2, 0, 1);
this.View = grid;
}
The content page is this:
public class Navigation : ContentPage
{
ListView menu;
ProfileView profile;
DataTemplate viewTemplate;
List<MenuItem> items;
public Navigation ()
{
profile = new ProfileView ();
items = new List<MenuItem> {
new MenuItem ("menuTradeIconBig.png", "TRADE"),
new MenuItem ("menuProfileIconBig.png", "PROFILE"),
new MenuItem ("menuPositionsIconBig.png", "POSITIONS"),
};
menu = new ListView { RowHeight = 40 };
menu.Header = profile;
var viewTemplate = new DataTemplate (typeof(MenuItemCell));
menu.ItemTemplate = viewTemplate;
menu.ItemsSource = items;
Content = new StackLayout {
Children = {
menu
}
};
}
The problem is that when I click the button that is responsible for opening the content page, I get a
java.lang.reflect.InvocationTargetException
If I comment the menu.ItemTemplate = viewTemplate;
, the page loads, but in the list view all items have the text MyProjectName.MenuItem
.
1 year later, I got back to this question and found the problem in 5 seconds:
This line
menuImage.SetBinding (ImageCell.ImageSourceProperty, "image");
Should be like this
menuImage.SetBinding (Image.ImageSourceProperty, "image");