如何将 StackLayout 添加到 activity

How to add StackLayout to an activity

我是 Xamarin 和移动开发的新手,我无法将堆栈布局加载到 activity。所以我在 class:

中有这个布局
public class TestStackLayout : ContentPage
{
    public TestStackLayout ()
    {
        StackLayout stackLayout = new StackLayout
        {
            Spacing = 0,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children = 
            {
                new Label
                {
                    Text = "StackLayout"
                }
            }
        };
    }
}

还有这个activity,在mainactivity中用一个按钮调用:

[Activity (Label = "TestActivity")] 
public class TestActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
    }
}

我的问题是:如何将我的 TestStackLayout 添加到 TestActivity?

您的 TestStackLayout 被定义为 ContentPage 而不是布局本身。因此它会在您的应用程序(在每个平台上)上显示一个页面。

当您使用 Xamarin.Forms 时,您不需要为每个页面添加 Activity(这就是关键)。您只需要一个加载 Xamarin.Forms 的 MainActivity 和应用程序。剩下的由 Xamarin.Forms.

完成

只需直接查看 this sample from Xamarin. You can dive into the code 即可了解发生了什么。

从 Xamarin.Forms 开始阅读 this introduction guide