Xamarin 创建一个按钮例子

点击按钮生成一个 Label,内容为当前时间,并且加载到一个ScrollView 对象上面:

using System;

using Xamarin.Forms;

namespace one
{
    public class ButtonLoggerPage : ContentPage
    {
        StackLayout loggerLayout = new StackLayout();

        public ButtonLoggerPage()
        {
            Button button = new Button { 
                Text = "Log the Click Time"
            };
            button.Clicked += OnButtonClicked;


            switch (Device.RuntimePlatform) {
                case Device.iOS :
                    Padding = new Thickness(5, 20, 5, 0);
                    break;
            }


            Content = new StackLayout
            {
                Children = {
                    button,
                    new ScrollView {
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        Content = loggerLayout
                    }
                }
            };
        }

        void OnButtonClicked(object sender, EventArgs args)
        {
            loggerLayout.Children.Add(new Label {
                Text = "Button clicked at " + DateTime.Now.ToString("T")
            });
        }
    }
}

效果如下: