2018年7月1日日曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の4章(型と参照 - 10時です。データがどこにあるかわかりますか?)、エクササイズ(p. 133)を取り組んでみる。

コード

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 Text="始点km" />
        <Slider x:Name="startingMileage"
                Maximum="999999"
                Minimum="1"/>
        <Label Text="終点km" />
        <Slider x:Name="endingMileage"
                Maximum="999999"
                Minimum="1"/>
        <Label Text="返済額"/>
        <Label x:Name="label4"
               FontSize="12"
               FontAttributes="Bold"
               Text="label4" />
        <Button x:Name="button1"
                Text="計算"
                Clicked="button1_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
 {
        private double milesTraveled;
        private double reimburseRate = 18;
        private double amountOwed;

  public MainPage()
  {
   InitializeComponent();
  }

        private void button1_Clicked(object sender, EventArgs e)
        {
            if (startingMileage.Value < endingMileage.Value)
            {
                milesTraveled = endingMileage.Value - startingMileage.Value;
                amountOwed = milesTraveled * reimburseRate;
                label4.Text = amountOwed + "円";
            }
            else
            {
                DisplayAlert(
                    "走行kmを計算できません",
                    "始点kmは、終点kmよりも小さい数字でなければなりません。",
                    "OK");
            }

        }
    }
}

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 = "走行km数計算"
            };
  }

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

コメントを投稿