namespaces and classes - Organizing your code - using directive
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.155(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>
コード
MainPage.xaml.cs
using PickRandomCards;
namespace PickRandomCardsMAUI;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void PickCardsButton_Clicked(object sender, EventArgs e)
{
if (int.TryParse(NumberOfCards.Text, out int numberOfCards))
{
string[] cards = CardPicker.PickSomeCards(numberOfCards);
PickedCards.Text = string.Empty;
foreach (string card in cards)
{
PickedCards.Text += card + Environment.NewLine;
}
PickedCards.Text += $"{Environment.NewLine}You picked {numberOfCards} cards.";
}
else
{
PickedCards.Text = "Please enter a valid number.";
}
}
}