mk-toolブログ

エンジニアと家のことをごちゃごちゃと書いてます

【Xamarin】邪道だけど試してみる3

Appメソッドの中に書く際に、Childの中に書くだけではボタンを押したときのイベントを指定できないじゃん
という風に考えていたが、以下のようにして打開。

		public App ()
		{
			var button = new Button {
				Text = "Click Me!",
				FontSize = 30,
				HorizontalOptions = LayoutOptions.Center,//中央に配置する(横方向)
				VerticalOptions = LayoutOptions.CenterAndExpand // 中央に配置する(縦方向)
			};
			// クリック時のイベント
			button.Clicked += (s, e) => {
				// ボタンの文字を変更する
				button.Text = "Developers.IO";
			};
				
			// The root page of your application
			MainPage = new ContentPage {
				Content = new StackLayout {
					VerticalOptions = LayoutOptions.Center,
					Children = {
						new Label {
							XAlign = TextAlignment.Center,
							Text = "Welcome to Xamarin Forms!"
						},
						button
					}
				}
			};
		}

Childの中にコントロールの変数を渡せば良いみたいですね。