開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の4章(型と参照 - 10時です。データがどこにあるかわかりますか?)、エクササイズ(p. 145)を取り組んでみる。
コード
MainPage.xaml
<Page
x:Class="App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Button x:Name="button1" Content="リロイド" HorizontalAlignment="Center" VerticalAlignment="Top" Click="button1_Click"/>
<Button x:Name="button2" Content="ルシンダ" HorizontalAlignment="Center" VerticalAlignment="Center" Click="button2_Click"/>
<Button x:Name="button3" Content="交換" HorizontalAlignment="Center" VerticalAlignment="Bottom" Click="button3_Click"/>
</Grid>
</Page>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.ViewManagement;
using Windows.UI.Popups;
// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください
namespace App
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
Elephant lloyd;
Elephant lucinda;
public MainPage()
{
this.InitializeComponent();
ApplicationView.GetForCurrentView().Title = "交換";
lloyd = new Elephant("リロイド", 85);
lucinda = new Elephant("ルシンダ", 100);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
lloyd.WhoAmIAsync();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
lucinda.WhoAmIAsync();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
Elephant t = lloyd;
lloyd = lucinda;
lucinda = t;
}
}
public class Elephant
{
private string name;
private int earSize;
public Elephant(string name, int earSize)
{
this.name = name;
this.earSize = earSize;
}
public async void WhoAmIAsync()
{
MessageDialog md =
new MessageDialog(
"私の耳の大きさは" + earSize + "cmです。",
name + "が言った・・・");
await md.ShowAsync();
}
}
}
0 コメント:
コメントを投稿