ボタンをタッチして表示するプログラム。
Visual Studio 2019でプロジェクトを作成する。
Xamarin Formsを選択する。
Windows APPを作成するような感覚で開発できる。
空白のプロジェクトを作成する。
MainPage.xamlを開いてButtonを作成します。
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?><ContentPagexmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"xmlns:d="http://xamarin.com/schemas/2014/forms/design"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"x:Class="HelloButton.MainPage"><StackLayout><!-- Place new controls here --><LabelText="Welcome to Xamarin.Forms!"HorizontalOptions="Center"VerticalOptions="CenterAndExpand"/></StackLayout></ContentPage>
1.Labelをプログラムから扱うために名前を付けます。
2.Buttonとイベントを定義します。
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?><ContentPagexmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"xmlns:d="http://xamarin.com/schemas/2014/forms/design"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"x:Class="HelloButton.MainPage"><StackLayout><!-- Place new controls here --> <!--x:Name="label" 名前を定義する。--><Labelx:Name="label"Text="Welcome to Xamarin.Forms!"HorizontalOptions="Center"VerticalOptions="CenterAndExpand"/> <!--ボタンとイベントを定義する--> <ButtonText="Touch Me"Clicked="BtnClick"/></StackLayout></ContentPage>
ボタンのイベントハンドラを追加します
MainPage.xaml.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingXamarin.Forms;namespaceHelloButton{// Learn more about making custom code visible in the Xamarin.Forms previewer// by visiting https://aka.ms/xamarinforms-previewer[DesignTimeVisible(false)]publicpartialclassMainPage:ContentPage{publicMainPage(){InitializeComponent();}//イベントハンドラの定義追加voidBtnClick(objects,EventArgse){//ラベルのTextに時刻を表示します。label.Text=DateTime.Now.ToString("HH:mm:ss");}}}
文字の大きさを変えてみる。
MainPage.xaml.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingXamarin.Forms;namespaceHelloButton{// Learn more about making custom code visible in the Xamarin.Forms previewer// by visiting https://aka.ms/xamarinforms-previewer[DesignTimeVisible(false)]publicpartialclassMainPage:ContentPage{publicMainPage(){InitializeComponent();}//イベントハンドラの定義追加voidBtnClick(objects,EventArgse){//ラベルのTextに時刻を表示します。label.FontSize=64;//font size指定label.Text=DateTime.Now.ToString("HH:mm:ss");}}}
改造して繰り返し時計を表示する
MainPage.xaml.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingXamarin.Forms;namespaceHelloButton{// Learn more about making custom code visible in the Xamarin.Forms previewer// by visiting https://aka.ms/xamarinforms-previewer[DesignTimeVisible(false)]publicpartialclassMainPage:ContentPage{publicMainPage(){InitializeComponent();}//イベントハンドラの定義追加voidBtnClick(objects,EventArgse){Buttonb=sasButton;label.FontSize=64;b.IsEnabled=false;// タイマーインタラプトルーチンDevice.StartTimer(TimeSpan.FromSeconds(1),()=>{label.Text=DateTime.Now.ToString("HH:mm:ss");returntrue;});}}}