Expander

该 Expander 控件提供可扩展容器来托管任何内容。 控件有两个主要属性用于存储内容:

Header此属性可以随任何视图一起提供,以允许完全自定义。 Header 将始终可见,与其交互(单击或轻触)将显示/折叠 Content。

注释

不建议将控件置于允许用户交互的标头中。

Content

这是在与 Header 属性交互(点击或轻触)时,或在 IsExpanded 属性被修改时将显示的主要内容。

注释

Expander 如果放置在 ListView 或 CollectionView 内部,可能会出现异常。 但是,可以通过设置 public Action<TappedEventArgs>? HandleHeaderTapped { get; set; }自定义实现来控制它。 每次点击标头时都会执行此操作。 请注意,更改此操作后,所有平台上的 CollectionView 和 ListView 可能会出现不同的表现。

处于折叠和展开状态的展开器屏幕截图

基本用法

以下示例展示了如何使用 Expander 视图:将 Header 属性设置为 Label 控件,并将 Content 设置为一个 HorizontalStackLayout,其内部包含 Image 和 Label。

XAML

包括 XAML 命名空间

若要在 XAML 中使用工具包,需要将以下 xmlns 添加到页面或视图中:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

因此,以下内容:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

</ContentPage>

将被修改为包括 xmlns,如下所示:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    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">

</ContentPage>

使用扩展器

以下示例演示如何在 XAML 中添加 Expander 视图。

<toolkit:Expander>
    <toolkit:Expander.Header>
        <Label Text="Baboon"
               FontAttributes="Bold"
               FontSize="Medium" />
    </toolkit:Expander.Header>
    <HorizontalStackLayout Padding="10">
        <Image Source="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg"
               Aspect="AspectFill"
               HeightRequest="120"
               WidthRequest="120" />
        <Label Text="Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae."
               FontAttributes="Italic" />
    </HorizontalStackLayout>
</toolkit:Expander>

C#

以下示例演示如何在 C# 中添加 Expander 视图。

using CommunityToolkit.Maui.Views;

var expander = new Expander
{
    Header = new Label
    {
        Text = "Baboon",
        FontAttributes = FontAttributes.Bold,
        FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
    }
};

expander.Content = new HorizontalStackLayout
{
    Padding = new Thickness(10),

    Children =
    {
        new Image
        {
            Source = "http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg",
            Aspect = Aspect.AspectFill,
            HeightRequest = 120,
            WidthRequest = 120
        },

        new Label
        {
            Text = "Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.",
            FontAttributes = FontAttributes.Italic
        }
    }
};

C# 标记

using CommunityToolkit.Maui.Views;

Content = new Expander
{
    Header = new Label()
        .Text("Baboon")
        .Font(bold: true, size: 18),

    Content = new HorizontalStackLayout
    {
        new Image()
            .Source("http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg")
            .Size(120)
            .Aspect(Aspect.AspectFill),

        new Label()
            .Text("Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.")
            .Font(italic: true)

    }.Padding(10)

}.CenterHorizontal();

属性

财产 类型 Description
Command ICommand 轻点 Expander 标头时执行。
CommandParameter object 传递给 Command 的参数。
Direction ExpandDirection 定义扩展器方向。
Content IView? 定义展开时 Expander 要显示的内容。
Header IView? 定义标头内容。
IsExpanded bool 确定 Expander 是否已展开。 此属性使用 TwoWay 绑定模式,默认值为 false.

ExpandDirection 枚举定义了以下成员:

价值 Description
Down 指示 Expander 内容位于标头下。
Up 指示 Expander 内容在标头上方。

该 Expander 控件还定义了一个 ExpandedChanged 事件,该事件会在轻触 Expander 标头时触发。

ExpandedChangedEventArgs

包含 ExpanderIsExpanded 状态的事件参数。

属性

财产 类型 Description
IsExpanded bool 确定 Expander 是否已展开。

示例

可以在 .NET MAUI 社区工具包示例应用程序中找到此功能的示例。

API

可以在 Expander 上查找 的源代码。