計算機科学のブログ

variables, statements, and methods - Dive into C# code - Controls, Stepper, Label, Slider

Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 2(variables, statements, and methods - Dive into C# code)、p.101(Exercise)の解答を求めてみる。

Markup Language

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ExperimentWithControls.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Image
                Source="dotnet_bot.png"
                HeightRequest="185"
                Aspect="AspectFit"
                SemanticProperties.Description="dot net bot in a race car number eight"/>

            <Label
                Text="Hello, World!"
                Style="{StaticResource Headline}"
                SemanticProperties.HeadingLevel="Level1"/>

            <Label
                Text="Welcome to &#10;.NET Multi-platform App UI"
                Style="{StaticResource SubHeadline}"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"/>

            <Button
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Fill"/>
            <Stepper
                Minimum="1"
                Maximum="10"
                Increment="1"
                SemanticProperties.Description="Stepper"
                ValueChanged="Stepper_ValueChanged"/>
            <Label Text="Here's the stepper value:"
                   SemanticProperties.Description="Here's the stepper value"/>
            <Label x:Name="StepperValue"
                   SemanticProperties.Description="Stepper value"
                   BackgroundColor="LightBlue"/>
            <Slider Minimum="0"
                    Maximum="1"
                    SemanticProperties.Description="Slider"
                    ValueChanged="Slider_ValueChanged"/>
            <Label Text="Here's the slider value:"
                   SemanticProperties.Description="Here's the slider value"/>
            <Label x:Name="SliderValue"
                   SemanticProperties.Description="Slider value"
                   BackgroundColor="LightBlue"/>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

コード

MainPage.xaml.cs

namespace ExperimentWithControls;

public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();
	}

	private void OnCounterClicked(object sender, EventArgs e)
	{
		count++;

		if (count == 1)
			CounterBtn.Text = $"Clicked {count} time";
		else
			CounterBtn.Text = $"Clicked {count} times";

		SemanticScreenReader.Announce(CounterBtn.Text);
	}

	private void Stepper_ValueChanged(object sender, ValueChangedEventArgs e)
	{
		StepperValue.Text = e.NewValue.ToString();

	}

	private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
	{
		SliderValue.Text = e.NewValue.ToString();
	}
}