計算機科学のブログ

namespaces and classes - Organizing your code - .NET MAUI App, AppShell.xaml, ShellContent, Controls, Entry, Button, Label

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 3(namespaces and classes - Organizing your code)、p.151(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="PickRandomCardsMAUI.MainPage">

       <ScrollView>
              <VerticalStackLayout
                     Padding="30,0"
                     Spacing="25">
                     <Label Text="How many cards should I pick?"
                            FontSize="18"
                            SemanticProperties.Description="How many cards should I pick?"
                            HorizontalOptions="Center"/>
                     <Entry x:Name="NumberOfCards"
                            Placeholder="Enter the number of cards to pick"
                            SemanticProperties.Description="Enter the number of cards to pick"/>
                     <Button x:Name="PickCardsButton"
                             Text="Pick some cards"
                             Clicked="PickCardsButton_Clicked"
                             HorizontalOptions="Center"
                             SemanticProperties.Hint="Pick some cards"/>
                     <Label x:Name="PickedCards"
                            Padding="20"
                            TextColor="White"
                            BackgroundColor="DarkBlue"
                            SemanticProperties.Description="picked cards"/>
              </VerticalStackLayout>
       </ScrollView>

</ContentPage>

AppShell.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="PickRandomCardsMAUI.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:PickRandomCardsMAUI"
    Shell.FlyoutBehavior="Disabled"
    TitleColor="White"
    BackgroundColor="Blue"
    Title="PickRandomCardsMAUI">

    <ShellContent
        Title="Pick a card!"
        ContentTemplate="{DataTemplate local:MainPage}"
        Route="MainPage"/>

</Shell>

コード

MainPage.xaml.cs

namespace PickRandomCardsMAUI;

public partial class MainPage : ContentPage
{
	public MainPage()
	{
		InitializeComponent();
	}

	private void PickCardsButton_Clicked(object sender, EventArgs e)
	{
	}
}