Xamarin 社区工具包 StateLayout 未显示

Xamarin Community Toolkit StateLayout not showing

我正在尝试为 Xamarin Forms 页面实现 StateLayout,如下所述: https://docs.microsoft.com/en-us/xamarin/community-toolkit/views/statelayout

但是,我无法显示加载状态视图。

重现步骤:

  1. 创建 Xamarin Forms 空白应用。
  2. 添加 Xamarin 社区工具包参考。
  3. 使用此代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="App4.MainPage">

    <Grid xct:StateLayout.CurrentState="Loading">
        <xct:StateLayout.StateViews>
            <xct:StateView StateKey="Loading">
                <Grid BackgroundColor="White">
                    <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                        <ActivityIndicator Color="#1abc9c" />
                        <Label TextColor="Red" FontSize="50" Text="Loading..." HorizontalOptions="Center" />
                    </StackLayout>
                </Grid>
            </xct:StateView>
        </xct:StateLayout.StateViews>

        <Label Text="Loaded" />

    </Grid>

</ContentPage>

我得到的不是“正在加载”标签:

我做错了什么?

根据你显示的文档,应该在StateLayout.CurrentState属性上使用Binding,然后在对应的cs页面上创建一个LayoutState类型的变量,并使用CurrentState = LayoutState.Loading;赋值正常显示的值。