代码之家  ›  专栏  ›  技术社区  ›  KalleP

将ListView和StackLayout添加到代码隐藏中的帧(不是XAML)

  •  1
  • KalleP  · 技术社区  · 7 年前

    我有这行代码在框架中添加列表视图:

    frame.Content = containerListView;
    

    但是,在stacklayout中还有一个按钮,我也希望它包含在框架中。

    我试过了,但没有成功:

    frame.Content = containerListView && buttonStackLayout;
    frame.Content = containerListView , buttonStackLayout;
    

    这与另一个stack post的情况不同,因为另一个post向viewcell添加了两个stacklayouts。我需要添加一个ListView和StackLayout到框架内容。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sayo Komolafe    7 年前

    首先,你需要创造 StackLayout Button 然后添加它们作为

    //StackLayout
    StackLayout buttonStack = new StackLayout()
    {
            Padding = new Thickness(0, 10),
            HorizontalOptions = LayoutOptions.FillAndExpand,
            BackgroundColor = "Gray" ,
    };
    
    //Button
    Button btn = new Button() { Text = "Button", HorizontalOptions = LayoutOptions.Center};
    btn.Clicked += btn_Clicked; //don't forget to create the btn_Clicked event/method
    
    buttonStack.Children.Add(btn);
    

    编辑

    private ListView GetListView(int index)
        {
            ListView listView = new ListView();
            listView.ItemsSource = //your list Source
    
            Frame frame = new Frame();
            frame.Margin = new Thickness(3);
    
            frame.Content = listView;
            return listView;
        }