Xamarin - 发布后刷新列表视图

乔苏·马丁内斯

我正在开发Xamarin Forms App,它有一个标签页。其中一个选项卡包含一个ListView控件,显示从 Rest API 消耗的数据。如果我点击添加按钮,一个模式会显示我可以在哪里创建新项目并将帖子发送到 Rest API。成功后,我从导航中弹出模态,留下ListView显示内容。在 my 中ListView,最后添加的项目没有显示。

ListView添加项目后,如何刷新调用 Rest API?

这是我的 XAML 页面:BonosView.xaml

<?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:iconize="clr-namespace:FormsPlugin.Iconize;assembly=FormsPlugin.Iconize"
    x:Class="rodriguez.BonosView" x:Name="BonosView"
    Title="Bonos">

    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0" />
    </ContentPage.Padding>

    <ContentPage.Content>
        <StackLayout>
            <Label x:Name="BonosListMessage" IsVisible="false" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Text="No exixten bonos para mostrar"/>
            <ListView x:Name="BonosList" ItemTapped="ViewDetails" RowHeight="70" >
                <ListView.ItemTemplate>
                  <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.Padding>
                                    <OnPlatform x:TypeArguments="Thickness" iOS="10,5" Android="10,5"/>
                                </Grid.Padding>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <Label x:Name="monto" Text="{Binding Monto}" Style="{DynamicResource TitleListBono}" Grid.Row="0" Grid.ColumnSpan="2" LineBreakMode="NoWrap" HorizontalOptions="StartAndExpand"/>
                                <Label x:Name="nombre" Text="{Binding nombreCompleto}" FontSize="Small" FontAttributes="Bold" Grid.Row="1" Grid.Column="0" HorizontalOptions="StartAndExpand"/>
                                <Label x:Name="estado" Text="{Binding Estado}" FontSize="Small" Grid.Row="1" Grid.Column="1" HorizontalOptions="End" TextColor="Green"/>
                                <Label x:Name="montoRD" Text="{Binding MontoRD}" FontSize="Small" Grid.Row="2" Grid.Column="0" HorizontalOptions="StartAndExpand"/>
                                <Label x:Name="fecha" Text="{Binding fechaCompra, StringFormat='{0:dd-MMM-yyyy}'}" FontSize="Small" Grid.Row="2" Grid.Column="1" HorizontalOptions="End"/>
                            </Grid>
                        </ViewCell>
                  </DataTemplate>
                </ListView.ItemTemplate>
              </ListView>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这是我用来填充列表的方法:

async void refreshData()
{
    this.IsBusy = true;
    bonosLista = await manager.GetAll();  //obtaining bonos from Server

    if (bonosLista != null)
    {
        if (bonosLista.Count() > 0)
        {
            BonosList.ItemsSource = bonosLista;
        }
        else
        {
            BonosList.IsVisible = false;
            BonosListMessage.IsVisible = true;
        }
    }
    else
    {
        await DisplayAlert("Error!", "Se ha producido un error en la conexión", "OK");
    }

    this.IsBusy = false;
}

我想在模态弹出后调用该方法。

恩科西

使用Appearing事件是正确的,但async void除非它是事件处理程序,否则您也不需要在方法上使用

refreshData要使用的更新方法async Task

async Task refreshData() {
    this.IsBusy = true;
    bonosLista = await manager.GetAll();  //obtaining bonos from Server

    if (bonosLista != null) {
        if (bonosLista.Count() > 0) {
            BonosList.ItemsSource = bonosLista;
        } else {
            BonosList.IsVisible = false;
            BonosListMessage.IsVisible = true;
        }
    } else {
        await DisplayAlert("Error!", "Se ha producido un error en la conexión", "OK");
    }
    this.IsBusy = false;
}

事件处理程序是唯一可以使用 async void 的地方,因此更新到

this.Appearing += async (object sender, EventArgs e) => {
    await refreshData();
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章