2018年5月29日火曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の2章(あくまでコードである - 内部では)、エクササイズ(p. 75)を取り組んでみる。

コード

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 Content="チェックボックスがONのとき色が変わる" HorizontalAlignment="Left" Margin="73,312,0,0" VerticalAlignment="Top" Height="188" Width="483" Click="Button_ClickAsync"/>
        <CheckBox x:Name="checkBox1" Content="色の変更を有効にする" HorizontalAlignment="Left" Margin="686,406,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Margin="145,572,0,0" Text="色を変える場合ボタンを押す" VerticalAlignment="Top" Height="89" Width="1129" BorderBrush="#00000000" Background="Red"/>
    </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.Popups; // MessageDialog

// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください

namespace App
{
    /// <summary>
    /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        // フォームアプリケーションと違うみたい
        // 動くことを優先。もっとコードを短くかける方法があるかも
        private bool isRed = true;
        SolidColorBrush red = new SolidColorBrush(Windows.UI.Colors.Red);
        SolidColorBrush blue = new SolidColorBrush(Windows.UI.Colors.Blue);
        MessageDialog md = new MessageDialog("チェックボックスがOFFです");
        private async void Button_ClickAsync(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked == true)
            {
                if (isRed)
                {
                    textBox1.Background = blue;
                    isRed = false;
                }
                else
                {
                    textBox1.Background = red;
                    isRed = true;
                }
            }
            else
            {
                await md.ShowAsync();
            }
        }
    }
}

0 コメント:

コメントを投稿