2017-08-10 125 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(); 
};