Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Dynamic Add Item to ListView, what's wrong

$
0
0

why the listView show in one row? I clicked 3 times. it means 3 row, but i only see one row. why?

//-------------- here is the code---------------------------

class ServerModel
{
public string Icon { get; set; }
public String ServerName { get; set; }
public string ErrorReson { get; set; }
}

class ServerPage : ContentPage
{
    public ServerPage()
    {
        var layout = new StackLayout
        {
            Orientation = StackOrientation.Vertical,
            Spacing = 10,
        };

        var btn = new Button() { Text = "Scan Server" };
        btn.Clicked += btn_Clicked;
        layout.Children.Add(btn);

        var title = new Label() { Text = "Server List" };
        layout.Children.Add(title);
        layout.Children.Add(scrollView);
        Content = layout;
    }

    private int index = 0;
    void btn_Clicked(object sender, EventArgs e)
    {
        listView_serverList.ItemsSource = null;
        index++;
        serverList.Add(new ServerModel() { Icon = "icon.png", ServerName = index.ToString() + ":192.168.0.87", ErrorReson = "ok" });
        listView_serverList.ItemsSource = serverList;
        scrollView.ForceLayout();
    }

    private ScrollView scrollView = new ScrollView
    {
        Padding = 2,
        Content = listView_serverList,
        BackgroundColor = Color.White,
        VerticalOptions = new LayoutOptions(LayoutAlignment.Fill, true)
    };

    private static ListView listView_serverList = new ListView
    {
        ItemsSource = serverList,
        ItemTemplate = new DataTemplate(() =>
        {
            var serverName = new Label();
            serverName.SetBinding(Label.TextProperty, new Binding("ServerName"));
            var errorReson = new Label() { };
            errorReson.SetBinding(Label.TextProperty, new Binding("ErrorReson"));
            var icon = new Image();
            icon.SetBinding(Image.SourceProperty, new Binding("Icon"));

            var grid = new Grid()
            {
                ColumnSpacing = 0,
                RowSpacing = 0,
                BackgroundColor = Color.White
            };
            ////-----------------------------
            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30, GridUnitType.Absolute) });
            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20, GridUnitType.Absolute) });
            ////-----------------------------
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(5, GridUnitType.Absolute) });  //logo
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50, GridUnitType.Absolute) }); //logo
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });      //area name


            grid.Children.Add(icon, 1, 2, 0, 2); // Left, First element//icon
            grid.Children.Add(serverName, 2, 3, 0, 1); // Right, First element//name
            grid.Children.Add(errorReson, 2, 1); //error

            //======== make view Cell =========
            return new ViewCell()
            {
                View = grid
            };
        })
    };

    private static List<ServerModel> serverList = new List<ServerModel>();
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>