開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の4章(型と参照 - 10時です。データがどこにあるかわかりますか?)、自分で考えてみよう(p. 155)を取り組んでみる。
コード
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" /> --> <Button x:Name="button1" Text="button1" 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 { public MainPage() { InitializeComponent(); } private void button1_Clicked(object sender, EventArgs e) { Elephant[] elephants = { new Elephant(){name="リロイド", earSize=100}, new Elephant(){name="ルシンダ", earSize=85 }, new Elephant(){name="ラリー", earSize=105 }, new Elephant(){name="ルシール", earSize=82 }, new Elephant(){name="ラース", earSize=110 }, new Elephant(){name="リンダ", earSize=93 }, new Elephant(){name="ハンフリー", earSize=113 }, }; Elephant biggestEars = elephants[0]; string result = ""; int[] vs = { 100, 105, 105, 110, 110, 113 }; for (int i = 1; i < elephants.Length; i++) { if (elephants[i].earSize > biggestEars.earSize) { biggestEars = elephants[i]; } result += (vs[i - 1] == biggestEars.earSize) + "\n"; } DisplayAlert( "", biggestEars.earSize.ToString() + "\n" + result, "OK"); } } public class Elephant:ContentPage { public string name; public int earSize; public void WhoAmI() { DisplayAlert( name + "が言った・・・", "私の耳の大きさは" + earSize + "cmです。", "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 = "" }); } 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 コメント:
コメントを投稿