Xamarin将动态图像列表绑定到网格

时间:2017-05-31 08:58:24

标签: c# android xamarin xamarin.forms

首先对这个烂摊子感到抱歉,我已经在这里开了一段时间了,我在xamarin中是个新人,从困难的问题开始。我试图在网格中动态添加随机图像列表。

目前我有网格打印像 Name1 Detail1 filename1 filename2 filename3 Name2 Detail2 Filename1 filename2

var cell = new DataTemplate(() => {
                var grid = new Grid() { Padding = 8, RowSpacing = 5, ColumnSpacing = 5 };
                grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
                grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50, GridUnitType.Star) });
  Subtitle.SetBinding(Label.TextProperty, "Images");
  grid.Children.Add(labelName, 0, 0);
                grid.Children.Add(labelDetail, 1, 0);
                grid.Children.Add(Subtitle, 1, 1);

最后的单元格附加到列表视图

 listdata.ItemTemplate = cell;

在对象中我已经将图像定义为字符串,我已经尝试创建另一个绑定到列表,并创建自定义调用的列表视图并将其添加到网格中,但是抛出错误,我可以'看到任何描述,它就像sigsegv-without-stack trace 还尝试代替网格,创建堆栈布局。再没什么。

CustomCell

           public class CustomNocCell : ViewCell
{

    public CustomCell()
    {
        //instantiate each of our views
        var image = new Image();
        var nameLabel = new Label();
        var typeLabel = new Label();
        var verticaLayout = new StackLayout();
        var horizontalLayout = new StackLayout() { BackgroundColor = Color.Transparent };

        //set bindings
        nameLabel.SetBinding(Label.TextProperty, new Binding("Name"));
        image.SetBinding(Image.SourceProperty, new Binding("Image"));

        //Set properties for desired design
        horizontalLayout.Orientation = StackOrientation.Horizontal;
        horizontalLayout.HorizontalOptions = LayoutOptions.Center;
        image.HorizontalOptions = LayoutOptions.End;
        nameLabel.FontSize = 24;

        //add views to the view hierarchy
        verticaLayout.Children.Add(nameLabel);
        horizontalLayout.Children.Add(verticaLayout);
        horizontalLayout.Children.Add(image);

        // add to parent view
        View = horizontalLayout;
    }
}

我已经尝试过的错误

   //ListView listView = new ListView();

        //listView.RowHeight = 60;
        //listView.ItemTemplate = new DataTemplate(typeof(CustomNocCell));
        ////listView.ItemsSource = "ImagesNoc";
        //listView.SetBinding(ListView.ItemsSourceProperty, "ImagesList");

ImagesList类

   [Ignore]
        public ListView ImagesList
        {
            get
            {


                ListView listView = new ListView();
                listView.RowHeight = 60;

                listView.ItemTemplate = new DataTemplate(typeof(CustomNocCell));



                List<string> list = new List<string>();

                if (!string.IsNullOrEmpty(Detail))
                {
                    foreach (string str in Detail.Split(';'))
                    {
                        list.Add(str);
                    }
                }



                listView.ItemsSource = list.Select(s => new { Name = s, Image = s.ToLower() + ".png" }).ToList();

                return listView;
            }

        }

我让这个类还返回了一个简单的类型名称和图像列表,CustomCell正在等待什么都没有。

修改

 public class TestObject
        {
            public string title { get; set; }
            public string countries { get; set; }

        }



        void load()
        {
            List<TestObject> list = new List<TestObject>();
            list.Add(new TestObject { title = "test1", countries = "esp,ita,prt" });
            list.Add(new TestObject { title = "test2", countries = "esp,ita" });
            list.Add(new TestObject { title = "test3", countries = "rus" });

            var labelTitle = new Label()
            {

                FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                VerticalOptions = LayoutOptions.StartAndExpand,
            };
            listdata = new ListView();
            listdata.ItemsSource = list;

           var cell = new DataTemplate(() => {
                var grid = new Grid() { Padding = 8, RowSpacing = 5, ColumnSpacing = 5 };
                grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                labelTitle.SetBinding(Label.TextProperty, "title");

                countriesLabel.SetBinding(Label.TextProperty, "countries"); //here i want to make a customcell to show the images, like be able to "bind" and calc the countries split the string by "," and add (name + ".png") then show the image. 

//当我在我的身边有源时,我能够在列表视图中完成它...在这一个我必须绑定然后计算..

                grid.Children.Add(labelTitle, 0, 0);
                grid.Children.Add(countriesLabel, 1, 0);
            });

            listdata.ItemTemplate = cell;
        }

0 个答案:

没有答案