星期五, 8月 04, 2017

[X.Form] Button - 計數

就根據這一篇 - Xamarin.Forms.Button Class 來練習,該範例是點擊 Buuton 計數

Xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                x:Class="XFViewPractice.XF_Button">
    <ContentPage.Content>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
            <Label Text="Button Demo" FontSize="Large" HorizontalOptions="Center"></Label>
            <Button 
                Text="Click Me!"
                FontSize="Large"
                BorderWidth="1"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"
                Clicked="Button_Clicked"></Button>
            <Label 
                x:Name="lblResult"
                Text="0 Button clicks" 
                FontSize="Large" 
                HorizontalOptions="Center" 
                VerticalOptions="CenterAndExpand"></Label>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
C#
namespace XFViewPractice
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class XF_Button : ContentPage
    {
        public XF_Button ()
        {
            InitializeComponent ();
            SetResult();
        }

        private int clickTotal = 0;
        private void Button_Clicked(object sender, EventArgs e)
        {
            clickTotal++;
            SetResult();
        }

        private void SetResult()
        {
            string s = clickTotal <= 1 ? "" : "s";
            lblResult.Text = $"{clickTotal} button click{s}";
        }
    }
}
執行結果-1

[X.Form] Button-1

執行結果-2

[X.Form] Button-2

沒有留言:

張貼留言