2017-08-10 127 views
1

我正在开发一个Xamarin表格应用程序,它有一个标签页。其中一个选项卡包含一个ListView控件,显示Rest API所消耗的数据。如果我点击添加按钮模式显示我可以创建新项目并发送到Rest API的帖子。成功后,我会弹出导航模式,其中显示ListView。在我的ListView中,最后添加的项目未显示。Xamarin - 后刷新列表视图

我添加项目后,如何管理刷新调用Rest API的ListView

这是我的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; 
} 

我想调用后,该方法莫代尔流行音乐。

+0

我会在Post完成后使用MessagingCenter发送消息,然后告诉LIstView刷新它的数据 – Jason

+0

如果您在模型中为BonosList.ItemsSource定义绑定为ObservableCollection,那么bonosLista然后列表应该更新 –

+0

它会在我按下名单@YuriS –

回答

1

使用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; 
} 

的事件处理程序的只有在异步空隙可以这么用的地方更新到

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

我结束了使用包含列表视图,所以当模态弹出我可以在方法内部刷新页面上Appearing方法。也许不是最好的解决方案,现在购买它的工作。

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