星期二, 8月 01, 2017

[X.Form] Switch

根據這篇 Xamarin.Forms.Switch Class 的練習

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_Switch">
    <ContentPage.Content>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
            <Label Text="Switch Demo" FontSize="Large" HorizontalOptions="Center"/>
            <Switch 
                HorizontalOptions="Center"
                x:Name="Switch1"
                WidthRequest="100"
                IsToggled="True"
                Toggled="Switch_Toggled"></Switch>
            <Label 
                HorizontalOptions="Center"
                x:Name="lblResult"
                Text="Switch 狀態" 
                FontSize="Large"></Label>
            </StackLayout>
    </ContentPage.Content>
</ContentPage>
C#
namespace XFViewPractice
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class XF_Switch : ContentPage
    {
        public XF_Switch()
        {
            InitializeComponent();

            SetToggled(Switch1.IsToggled);
        }

        private void Switch_Toggled(object sender, ToggledEventArgs e)
        {
            SetToggled(e.Value);
        }

        private void SetToggled(bool result)
        {
            lblResult.Text = $"Switch 狀態是 {result}";
        }
    }
}
執行結果

[X.Form] Switch-1

[X.Form] Switch-2

沒有留言:

張貼留言