開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(継承 - オブジェクトの系図)、エクササイズ(p. 178)を取り組んでみる。
コード
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage">
<ContentPage Title="夕食会">
<StackLayout>
<Label Text="参加人数"/>
<Slider x:Name="total1"
Maximum="20"
Value="5"/>
<Label Text="デコレーション" />
<Switch IsToggled="True"/>
<Label Text="ヘルシーオプション"/>
<Switch IsToggled="False" />
<Label Text="総額"/>
</StackLayout>
</ContentPage>
<ContentPage Title="誕生会">
<StackLayout>
<Label Text="参加人数"/>
<Label x:Name="label2"
Text="5人"/>
<Slider x:Name="numberBirthday"
Maximum="20"
Value="5"
ValueChanged="numberBirthday_ValueChanged"/>
<Label Text="デコレーション" />
<Switch x:Name="fancyBirthday"
IsToggled="True"
Toggled="fancyBirthday_Toggled"/>
<Label Text="ケーキに書き込む文字"/>
<Editor x:Name="cakeWriting"
Text="Happy Birthday"
TextChanged="cakeWriting_TextChanged"/>
<Label x:Name="birthdayCost"
Text="総額"/>
</StackLayout>
</ContentPage>
</TabbedPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App1
{
public partial class MainPage : TabbedPage
{
BirthdayParty birthdayParty;
public MainPage()
{
InitializeComponent();
birthdayParty = new BirthdayParty(
(int)numberBirthday.Value,
fancyBirthday.IsToggled,
cakeWriting.Text);
DisplayDinnerPartyCost();
}
private void numberBirthday_ValueChanged(object sender, ValueChangedEventArgs e)
{
label2.Text = numberBirthday.Value + "人";
birthdayParty.NumberOfPeople = (int)numberBirthday.Value;
DisplayDinnerPartyCost();
}
private void fancyBirthday_Toggled(object sender, ToggledEventArgs e)
{
birthdayParty.CalculateCostOfDecorations(fancyBirthday.IsToggled);
DisplayDinnerPartyCost();
}
private void cakeWriting_TextChanged(object sender, TextChangedEventArgs e)
{
birthdayParty.CakeWriting = cakeWriting.Text;
DisplayDinnerPartyCost();
}
private void DisplayDinnerPartyCost()
{
decimal cost = birthdayParty.CalculateCost(fancyBirthday.IsToggled);
birthdayCost.Text = cost.ToString("c"); ;
}
}
public class BirthdayParty: ContentPage
{
const int costOfFoodPerPerson = 25;
private int numberOfPeople;
private bool fancyDecorations;
private decimal costOfDecorations = 0;
private int cakeSize;
private string cakeWriting = "";
public BirthdayParty
(int numberOfPeople, bool fancyDecorations, string cakeWriting)
{
this.numberOfPeople = numberOfPeople;
CalculateCakeSize();
this.fancyDecorations = fancyDecorations;
this.cakeWriting = cakeWriting;
CalculateCostOfDecorations(fancyDecorations);
}
public int NumberOfPeople
{
get { return numberOfPeople; }
set
{
numberOfPeople = value;
CalculateCakeSize();
CalculateCostOfDecorations(fancyDecorations);
this.CakeWriting = cakeWriting;
}
}
public bool FancyDerocations
{
get { return fancyDecorations; }
set { fancyDecorations = value; }
}
public string CakeWriting
{
get { return this.cakeWriting; }
set
{
int maxLength = cakeSize == 7 ? 16 : 40;
if (value.Length > maxLength)
{
DisplayAlert(
"",
cakeSize + "号のケーキには文字が多すぎます。",
"OK");
if (maxLength > this.cakeWriting.Length)
{
maxLength = this.cakeWriting.Length;
}
this.cakeWriting = cakeWriting.Substring(0, maxLength);
}
else
{
this.cakeWriting = value;
}
}
}
private void CalculateCakeSize()
{
cakeSize = numberOfPeople <= 4 ? 7 : 15;
}
public void CalculateCostOfDecorations(bool fancy)
{
fancyDecorations = fancy;
if (fancy)
{
costOfDecorations = (numberOfPeople * 15.00M) + 50M;
}
else
{
costOfDecorations = (numberOfPeople * 7.50M) + 30M;
}
}
public decimal CalculateCost(bool fancy)
{
decimal totalCost =
costOfDecorations +
costOfFoodPerPerson * numberOfPeople;
decimal cakeCost =
cakeSize == 7 ?
400M + cakeWriting.Length * 25M :
7500M + cakeWriting.Length * 25M;
return totalCost + cakeCost;
}
}
}
App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.App">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace App1
{
public partial class App : Application
{
public App ()
{
InitializeComponent();
// MainPage = new MainPage();
MainPage = new NavigationPage(
new MainPage() { Title = "パーティーの計画" });
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
0 コメント:
コメントを投稿