5.Xamarin.Forms里按钮的实现

其实Xamarin.Forms中的按钮的用发和WPF的几乎一样。
先看看这个吧,创建一个按钮并添加点击事件。

class aaa:ContentPage
    {
        StackLayout a = new StackLayout();
        public aaa()
        {
            Button b = new Button
            {
                Text = "button"

            };
            b.Clicked += B_Clicked;
            Content = new StackLayout
            {
                Children =
                {
                    b,
                    new ScrollView
                    {
                        VerticalOptions=LayoutOptions.FillAndExpand,
                        Content=a
                    }
                }
            };
        }

        private void B_Clicked(object sender, EventArgs e)
        {
            a.Children.Add(new Label
            {
                Text = "Clicked at" + DateTime.Now.ToString("T")
            });
        }
    }

最终的效果
5.Xamarin.Forms里按钮的实现_第1张图片

当然也可以在代码中使用Lambda表达式。如下:

    class aaa:ContentPage
    {
        StackLayout a = new StackLayout();
        public aaa()
        {
            Button b = new Button
            {
                Text = "button"

            };
            b.Clicked += (s, e) =>
             {
                 a.Children.Add(new Label
                 {
                     Text = "Clicked at" + DateTime.Now.ToString("T")
                 });
             };
            Content = new StackLayout
            {
                Children =
                {
                    b,
                    new ScrollView
                    {
                        VerticalOptions=LayoutOptions.FillAndExpand,
                        Content=a
                    }
                }
            };
        }
    }

以下是Button可以设置的属性:

FontFamily 
FontSize 
FontAttributes 
TextColor 
BorderColor 
BorderWidth 
BorderRadius 
Image

下面展示的是两个按钮如何使用同一个事件,做不同的事:

 class aaa:ContentPage
    {
        StackLayout a = new StackLayout();
        Button b = new Button
        {
            Text = "Add",
            HorizontalOptions = LayoutOptions.CenterAndExpand
        };
        Button c = new Button
        {
            Text = "Remove",
            HorizontalOptions = LayoutOptions.CenterAndExpand
        };
        public aaa()
        {

            b.Clicked += B_Clicked;

            c.Clicked += B_Clicked;
            Content = new StackLayout
            {
                Children =
                {
                    new StackLayout
                    {
                        Orientation=StackOrientation.Horizontal,
                        Children=
                        {
                            b,c
                        }
                    },
                    new ScrollView
                    {
                        VerticalOptions=LayoutOptions.FillAndExpand,
                        Content=a
                    }
                }
            };
        }

        private void B_Clicked(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (button == b)
            {
                a.Children.Add(new Label
                {
                    Text = "Clicked at" + DateTime.Now.ToString("T")
                });
            }
            else
            {
                a.Children.RemoveAt(0);
            }

            c.IsEnabled = a.Children.Count > 0;
        }
    }

5.Xamarin.Forms里按钮的实现_第2张图片

你可能感兴趣的:(Xamarin)