Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9517

眺めて覚える C# Xamarin Button(2)

$
0
0

ボタンをタッチして表示するプログラム。

Visual Studio 2019でプロジェクトを作成する。

image.png
Xamarin Formsを選択する。
Windows APPを作成するような感覚で開発できる。
image.png
空白のプロジェクトを作成する。
image.png

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");}}}

実行結果は、
image.png
ボタンをタッチすると時刻が表示される。

文字の大きさを変えてみる。

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");}}}

image.png

改造して繰り返し時計を表示する

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;});}}}

実行すると時計を表示し続ける。
image.png


Viewing all articles
Browse latest Browse all 9517

Trending Articles