如何在xamarin表单中以编程方式创建一个单独的视图类?

时间:2016-08-29 10:35:16

标签: xamarin xamarin.forms xamarin-studio

我是Xamarin Forms的新手,我正在尝试创建一个视图并将按钮添加为视图的子视图。我无法将孩子添加到"查看"类。如何将子视图/子项添加到"查看"以编码方式编写的xamarin课程?

1 个答案:

答案 0 :(得分:0)

您必须使用布局(https://developer.xamarin.com/guides/xamarin-forms/controls/layouts/) 最简单的方法是使用StackLayouthttps://developer.xamarin.com/api/type/Xamarin.Forms.StackLayout/

StackLayout stackLayout = new StackLayout
{
    Spacing = 0,
    VerticalOptions = LayoutOptions.FillAndExpand,
    Children = 
    {
        new Button
        {
            Text = "Button 1",
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.CenterAndExpand
        },
        new Button
        {
            Text = "Button 2",
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.CenterAndExpand
        },
        new Button
        {
            Text = "Button 3",
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.CenterAndExpand
        }
    }
}