本記事の概要
この記事では Button をコードから動的に生成する方法について説明します。
公式サイト情報はコチラを参照してください。
ボタンを動的に生成する
ボタンは Button クラスを使用することで、動的に生成することができます。
以下はアプリが起動した時に、Button を動的に生成する例です。
5〜10行目で、ボタンを生成し、XAML に存在する myVStack というコントロールに追加をしています。
C# の例
public MainPage()
{
InitializeComponent();
Button button = new Button
{
Text = "Hello World!!",
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
this.myVStack.Add(button);
}
XAML の例
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiAppSample.MainPage">
<ScrollView>
<VerticalStackLayout x:Name="myVStack" Spacing="25" Padding="30" />
</ScrollView>
</ContentPage>
.NET MAUI Tips
本サイトでまとめている .NET MAUI Tips の一覧はこちらから確認できます
Please follow and like us:


コメント