ListView with DataTemplate - java.lang.reflect.InvocationTar



我有这个类:

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

和下面的ViewCell类:

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

内容页如下:

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

问题是,当我点击负责打开内容页的按钮时,我得到一个

java.lang.reflect.InvocationTargetException

如果我注释menu.ItemTemplate = viewTemplate;,页面加载,但在列表视图中所有项目都有文本MyProjectName.MenuItem

由于您没有提供ProfileView或更完整的异常跟踪的源,因此只能猜测,但从它的命名来看,看起来您正在将View设置为menu.Header,而它应该是一个视图模型,如MenuItem,因为您没有为它指定HeaderTemplate

首先,我要试试

menu.Header = new MenuItem ("foo.png", "HEADER");

这可能不会给你期望的视觉效果,但它应该不会再崩溃了。然后,你可以用HeaderTemplate配置标题的视觉效果,并将menu.Header赋值给一个类型为HeaderItem

的对象(尚未定义)。

你会得到更多关于这个主题的信息http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/listview/#headerfooter

在任何情况下,报告这个问题到http://bugzilla.xamarin.com都是值得的。即使你的代码是不正确的,由于一个错误的模型的模板,它不应该崩溃,

我认为是因为你试图尝试填充GridView到ListView项单元格。我不认为那样会有你想要的结果。你可以做的是在MenuItemCell中创建一个stackLayout,并将其方向设置为水平,这样你仍然可以达到同样的效果。

当你得到这些消息时,通常会抛出异常,但它不会显示在你谈论的弹出框中。因此,您需要查看应用程序输出,以确切了解发生了什么。

在我看来,我会将整个导航代码包装在一个try/catch块中,并在异常情况下使用Debug.WriteLine()方法输出消息和堆栈跟踪,以便您可以更好地处理错误。

如果在导航中没有抛出错误,那么可以放心地认为罪魁祸首在MenuItemCell类中,你可以重复上面相同的过程。

一年后,我回到这个问题,在5秒内发现了问题:

这条线

menuImage.SetBinding (ImageCell.ImageSourceProperty, "image");

应该是这样的

menuImage.SetBinding (Image.ImageSourceProperty, "image");

相关内容

最新更新