この記事では .NET MAUI で Expander を使う方法を紹介しています。
Expander を活用することで画面のスペースを節約でき、ユーザーが興味のあるコンテンツにアクセスしやすくするメリットがあります。
ぜひこの記事を参考にしてみて下さい。
![](https://marunaka-blog.com/wp-content/uploads/2023/04/732f4c1bbc64fca859d0210c642e2a3f-300x200.jpg)
オススメの参考書
丁寧に解説された1冊。リリースされて間もない.NET MAUI の基礎が学べるので、これから Android や iOS などをターゲットとして開発するなら非常に参考になる書籍です。
そもそも Expander とは?
![](https://marunaka-blog.com/wp-content/uploads/2022/03/e79f61a6baca46b24df8b1f92d141ae5-2.png)
Expander(エキスパンダー)は、コンテンツエリアの開閉が行えるコントロールです。
![](https://marunaka-blog.com/wp-content/uploads/2023/04/2f881b321ac76cdad312740462db34f1.gif)
Expander には、大きく分けてヘッダーとコンテンツの2つのプロパティがあります。ヘッダーは展開/折りたたみのトリガーとして機能します。コンテンツは展開/折りたたみのビューを指定できます。
このコントロールを使うことで必要な時にだけコンテンツを表示できるので、画面のヘッダーのタイトルからユーザーが興味あるコンテンツにアクセスしやすくなります。また、コンテンツの階層化や分類が可能になります。
Expanderを使うには?
![](https://marunaka-blog.com/wp-content/uploads/2022/03/091184bcda645b76415dad025b422929.jpg)
この Expander は .NET MAUI の標準コントロールとして登録されていません。そのため、Expander を扱うには「CommunityToolkit.Maui」というライブラリを NuGet からインストールする必要があります。
![](https://marunaka-blog.com/wp-content/uploads/2023/05/c572f4d2cb372aa02e615d355756e70a-300x200.jpg)
このライブラリは、.NET MAUI でアプリ開発する時に役立つことを1つにまとめています。ビヘイビア、コンバーターなどの汎用的な機能が揃っているのが特徴です。
\ GitHubサイト /
ライブラリのインストール
.NET MAUI Community Toolkit を .NET MAUI のプロジェクトにインストールします。
次の手順でプロジェクトへインストールしましょう。
お使いのパソコンにインストールされている Visual Studio 2022 で、プロジェクトを開きます。
統合開発環境である Visual Studio のインストールがまだの方は、次の記事を参考にしてインストールします。
![](https://marunaka-blog.com/wp-content/uploads/2022/11/5b06ffe152bc8aa87073943cbcb585ca-300x200.jpg)
メニューバーから [ツール] -> [NuGet パッケージ マネージャー] -> [ソリューションの NuGet パッケージの管理] の順に選択します。
検索欄に「CommunityToolkit.Maui」を入力して、検索結果の一覧から「CommunityToolkit.Maui」をインストールします。(2023年5月現在、バージョンは5.1.0)
![](https://marunaka-blog.com/wp-content/uploads/2023/04/17e80927439439f1ebd4f9b165ae6733.png)
ライセンスへの同意を求める画面が表示されるので、内容を確認して [I Accept] ボタンをクリックします。
![](https://marunaka-blog.com/wp-content/uploads/2023/04/de3d8beb945e75f5edfe5925dcc28ba4.png)
インストールが完了すると、ReadMe.txt が表示されます。このファイルにはライブラリの使い方が記載されています。
ReadMe.txt は閉じても特に問題はありません。
![](https://marunaka-blog.com/wp-content/uploads/2023/04/410dc18ea1b6b4b8cab40bb6d78095de.png)
ライブラリの初期設定
CommunityToolkit.Maui を使うための設定があります。
次の手順でライブラリの初期設定をしましょう。
MainProgram.cs を開いて、ファイルの先頭に using ステートメントを追加します。
using CommunityToolkit.Maui;
続いて、スタートアップ処理に CommunityToolkit.Maui の初期化処理を追加します。(下記コードの8行目に追加)
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
return builder.Build();
}
}
これで Expander を使う準備が整いました。
Expanderを使う方法
![](https://marunaka-blog.com/wp-content/uploads/2022/03/2ba5cbb657d2e1599edd30d8fa258ed4.jpg)
MainPage.xaml を開いて、XAML の名前空間に CommunityToolkit.Maui の名前空間を追加します。
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Expander は Header と Content に分けて記述します。ここでは Header 部分にタイトルを表示する Label を追加し、Content 部分にはアイテムを表示する Label を2つ追加します。
XAML は次のようになります。
<ContentPage
x:Class="Sample.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<toolkit:Expander>
<!-- ヘッダー -->
<toolkit:Expander.Header>
<Label
BackgroundColor="#F1F1F1"
FontAttributes="Bold"
FontSize="18"
Text="タイトル" />
</toolkit:Expander.Header>
<!-- コンテンツ -->
<toolkit:Expander.Content>
<VerticalStackLayout Margin="5">
<Label Text="アイテム1" />
<Label Text="アイテム2" />
</VerticalStackLayout>
</toolkit:Expander.Content>
</toolkit:Expander>
</ContentPage>
Windows Machine でデバッグを実行してみましょう。Expander のヘッダー部分をクリックすると、コンテンツが展開されます。
![](https://marunaka-blog.com/wp-content/uploads/2023/04/2f881b321ac76cdad312740462db34f1.gif)
デフォルトでは Content は折りたたみされた状態ですが、IsExpanded プロパティ(bool型)を true にすることで Content は展開された状態で表示されます。
サンプルコード
Expanderのサンプルコードを紹介します。XAMLは次の通りです。
<ContentPage
x:Class="Sample.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<toolkit:Expander x:Name="MyExpander" IsExpanded="true">
<!-- ヘッダー -->
<toolkit:Expander.Header>
<Grid
BackgroundColor="#F1F1F1"
ColumnDefinitions="*,30"
HeightRequest="35">
<Grid.Resources>
<Style TargetType="Label">
<Setter Property="FontSize" Value="18" />
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="VerticalTextAlignment" Value="Center" />
</Style>
</Grid.Resources>
<Label Margin="10,0,0,0" Text="タイトル" />
<Label Grid.Column="1" Text="∨">
<Label.Triggers>
<DataTrigger
Binding="{Binding Source={x:Reference MyExpander}, Path=IsExpanded}"
TargetType="Label"
Value="true">
<Setter Property="Text" Value="∧" />
</DataTrigger>
</Label.Triggers>
</Label>
</Grid>
</toolkit:Expander.Header>
<!-- コンテンツ -->
<toolkit:Expander.Content>
<Frame
Margin="5"
BorderColor="Gray"
CornerRadius="5">
<Grid ColumnDefinitions="60, *">
<Image
MaximumHeightRequest="50"
MaximumWidthRequest="50"
Source="person.png" />
<VerticalStackLayout Grid.Column="1" Margin="10,0,0,0">
<Label
FontSize="20"
Text="Tanaka Taro"
TextColor="#086bed" />
<Label Text="Engineer" />
<Label FontSize="12" Text="E-mail : xxx.xxxxx@gmail.com" />
</VerticalStackLayout>
</Grid>
</Frame>
</toolkit:Expander.Content>
</toolkit:Expander>
</ContentPage>
Windows Machine でデバッグを実行した結果が次になります。
![](https://marunaka-blog.com/wp-content/uploads/2023/04/0935ff0fd6960e695f3d1de09a66e19c.gif)
まとめ
![](https://marunaka-blog.com/wp-content/uploads/2023/04/c35f903f10ed2371757ca77d9f41dce9-1024x358.png)
この記事では、.NET MAUI で Expander を使用する方法を紹介しました。
必要な時にだけコンテンツを表示できる便利なコントロールなので、この記事が参考になれば幸いです。
以上、最後まで読んで頂きありがとうございました。
![](https://marunaka-blog.com/wp-content/uploads/2023/04/259c955856c5bdc619d88605c3a5c583-300x200.jpg)
![](https://marunaka-blog.com/wp-content/uploads/2022/03/2f5a8ba6b1682610f773da3b2f1516f9-300x169.jpg)
![](https://marunaka-blog.com/wp-content/uploads/2022/09/1c0278efd52188a8154c18b8b21f1be2-300x169.jpg)