2018年6月30日土曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の3章(オブジェクト指向になる! - わかりやすいコードにする)、エクササイズ(p. 117)を取り組んでみる。

コード

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:App1"
             x:Class="App1.MainPage">
    <StackLayout>
        <!-- Place new controls here -->
        <!--
     <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        -->
        <Label x:Name="joesCash"/>
        <Label x:Name="bobsCash" />
        <Label x:Name="bankCash" />
        <Button x:Name="button1"
                Text="ジョーに1000円渡す"
                Clicked="button1_Clicked"/>
        <Button x:Name="button2"
                Text="ボブから500円受け取る"
                Clicked="button2_Clicked"/>
        <Button x:Name="joeGivesToBob"
                Text="ジョーがボブに1000円渡す"
                Clicked="joeGivesToBob_Clicked"/>
        <Button x:Name="bobGivesToJoe"
                Text="ボブがジョーに500円渡す"
                Clicked="bobGivesToJoe_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 App1
{
 public partial class MainPage : ContentPage
 {
        Guy joe;
        Guy bob;
        int bank = 10000;

  public MainPage()
  {
   InitializeComponent();


            joe = new Guy() { name = "ジョー", cash = 5000 };
            bob = new Guy() { name = "ボブ", cash = 10000 };

            UpdatePage();
        }

        public void UpdatePage()
        {
            joesCash.Text = joe.name + "の所持金は" + joe.cash + "円";
            bobsCash.Text = bob.name + "の所持金は" + bob.cash + "円";
            bankCash.Text = "銀行の所持金は" + bank + "円";
        }
        private void button1_Clicked(object sender, EventArgs e)
        {
            if (bank >= 1000)
            {
                bank -= joe.ReceiveCash(1000);
                UpdatePage();
            }
            else
            {
                DisplayAlert("", "銀行にお金がありません", "OK");
            }
        }

        private void button2_Clicked(object sender, EventArgs e)
        {
            bank += bob.GiveCash(500);
            UpdatePage();
        }

        private void joeGivesToBob_Clicked(object sender, EventArgs e)
        {
            bob.ReceiveCash(joe.GiveCash(1000));
            UpdatePage();
        }

        private void bobGivesToJoe_Clicked(object sender, EventArgs e)
        {
            joe.ReceiveCash(bob.GiveCash(500));
            UpdatePage();
        }
    }
}

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 コメント:

コメントを投稿