開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(インタフェースと抽象クラス - クラスに約束を守らせる)、長いエクササイズ(p. 260)を取り組んでみる。
コード
MainPage.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" xmlns:local="clr-namespace:App2" x:Class="App2.MainPage"> <StackLayout> <Editor x:Name="description"/> <Button x:Name="goHere" Text="ここへ行け" Clicked="goHere_Clicked"/> <Picker x:Name="exits"/> <Button x:Name="goThroughTheDoor" Text="このドアを使え。" Clicked="goThroughTheDoor_Clicked"/> </StackLayout> </ContentPage>
MainPage.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; namespace App2 { public partial class MainPage : ContentPage { OutsideWithDoor frontYard; OutsideWithDoor backYard; Outside garden; RoomWithDoor livingRoom; Room diningRoom; RoomWithDoor kitchen; Location currentLocation; public MainPage() { InitializeComponent(); CreateObjects(); MoveToANewLocation(livingRoom); } private void CreateObjects() { frontYard = new OutsideWithDoor( "前庭", false, "真ちゅうのノブを持つオーク材のドア"); backYard = new OutsideWithDoor( "裏庭", true, "網戸"); garden = new Outside("庭園", false); livingRoom = new RoomWithDoor( "リビングルーム", "アンティークカーペット", "真ちゅうのノブを持つオーク材のドア"); diningRoom = new Room( "ダイニングルーム", "クリスタルのシャンデリア"); kitchen = new RoomWithDoor( "台所", "ステンレス製の電化製品", "網戸"); frontYard.exists = new Location[] { livingRoom, garden}; backYard.exists = new Location[] { diningRoom, garden }; garden.exists = new Location[] { frontYard, backYard }; livingRoom.exists = new Location[] { frontYard, diningRoom }; diningRoom.exists = new Location[] { livingRoom, kitchen }; kitchen.exists = new Location[] { diningRoom, backYard }; frontYard.DoorLocation = livingRoom; backYard.DoorLocation = kitchen; livingRoom.DoorLocation = frontYard; kitchen.DoorLocation = backYard; } private void MoveToANewLocation(Location location) { currentLocation = location; exits.Items.Clear(); foreach (var item in currentLocation.exists) { exits.Items.Add(item.Name); } exits.SelectedIndex = 0; description.Text = currentLocation.Description; goThroughTheDoor.IsVisible = currentLocation is IHasExteriorDoor ? true : false; } private void goHere_Clicked(object sender, EventArgs e) { MoveToANewLocation(currentLocation.exists[exits.SelectedIndex]); } private void goThroughTheDoor_Clicked(object sender, EventArgs e) { IHasExteriorDoor hasExteriorDoor = currentLocation as IHasExteriorDoor; MoveToANewLocation(hasExteriorDoor.DoorLocation); } } public interface IHasExteriorDoor { string DoorDescription { get; } Location DoorLocation { get; } } public class Room: Location { private string decoration; public Room(string name, string decoration): base(name) { this.decoration = decoration; } public string Decoration => decoration; } public class Outside: Location { private bool hot; public Outside(string name, bool hot): base(name) { this.hot = hot; } public bool Hot => hot; public override string Description { get { string description = base.Description; if (hot) { description += "ここはとても暑い。"; } return description; } } } public class OutsideWithDoor: Outside, IHasExteriorDoor { private Location location; private string doorDescription; public OutsideWithDoor( string name, bool hot, string doorDescription) :base(name, hot) { this.doorDescription = doorDescription; } public string DoorDescription => doorDescription; public Location DoorLocation { get => location; set => location = value; } } public class RoomWithDoor : Room, IHasExteriorDoor { private string doorDescription; private Location location; public RoomWithDoor( string name, string decoration, string doorDescription) : base(name, decoration) { this.doorDescription = doorDescription; } public string DoorDescription => doorDescription; public Location DoorLocation { get => location; set => location = value; } } }
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="App2.App"> <Application.Resources> </Application.Resources> </Application>
App.xaml.cs
using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompilationOptions.Compile)] namespace App2 { 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 コメント:
コメントを投稿