2017-09-28 2 views
0

Je travaille sur Xamarin, je dois afficher les données de base de données sur la charge de forme. Je souhaite donc afficher l'indicateur d'activité lorsque le fonctionnement de la base de données prend du temps.formes Xamarin forme activityindicator ne fonctionne pas dans le constructeur

Je suis en train ActivityIndicator true la charge du constructeur età la fin la mise à false.

Mais ce ne est pas montrer

Voici le code

Xaml est comme ci-dessous

<ContentPage.Content> 
    <StackLayout VerticalOptions="StartAndExpand" Padding="5,5,5,5"> 
    <ListView HasUnevenRows="True" RowHeight="100" HeightRequest="-1" x:Name="ListViewAppointments" VerticalOptions="StartAndExpand"> 
     <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
      <Grid Padding="20,0,20,0" ColumnSpacing="20"> 
       <Grid.RowDefinitions> 
       <RowDefinition Height="40"></RowDefinition> 
       <RowDefinition Height="40"></RowDefinition> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="40"></ColumnDefinition> 
       <ColumnDefinition Width="*"></ColumnDefinition> 
       <ColumnDefinition Width="40"></ColumnDefinition> 
       </Grid.ColumnDefinitions> 

       <!--<BoxView Color="#f7f7f7" Grid.Column="0" Grid.RowSpan="2"/> 
       <BoxView Color="#ffffff" Grid.Column="1" Grid.RowSpan="2"/>--> 
       <Image Grid.RowSpan="2" Grid.Column="0" Source="documenticon.png" Aspect="AspectFit"></Image> 
       <Label TextColor="#00344e" FontAttributes="Bold" Text="{Binding ReportName}" Grid.Row="0" Grid.Column="1" VerticalTextAlignment="End"></Label> 
       <Label TextColor="#0073ae" Text="{Binding PrintDate}" Grid.Row="1" Grid.Column="1" VerticalTextAlignment="Start"></Label> 
       <Image Grid.RowSpan="2" Grid.Column="2" Source="downloadicon.png" Aspect="AspectFit"></Image> 
      </Grid> 
      </ViewCell> 
     </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
    <ActivityIndicator x:Name="ActLoder" HorizontalOptions="CenterAndExpand" Color="#ffffff" VerticalOptions="CenterAndExpand" /> 
    </StackLayout> 
</ContentPage.Content> 

et le code derrière

public partial class CurrentDocuments : ContentPage 
{ 
    public CurrentDocuments() 
    { 
     InitializeComponent(); 
     new Task(Initializer).Start(); 
    } 
    public async void Initializer() 
    { 
     ShowLoader(true); 
     NavigationPage.SetBackButtonTitle(this, ""); 
     Title = "CURRENT DOCUMENTS"; 


     DocumentsResponse appointsData = null; 
     await Task.Run(async() => 
     { 
      appointsData = await GetCurrentDocuments(); 
     }).ContinueWith(_ => 
     { 
      Device.BeginInvokeOnMainThread(() => { 
       ListViewAppointments.ItemsSource = appointsData.ListOfDocuments; 
       ShowLoader(false); 
      }); 
     }); 
    } 

    private async Task<DocumentsResponse> GetCurrentDocuments() 
    { 
     DocumentManager manager = new DocumentManager(); 
     var result = await manager.GetCurrentDocuments(Application.Current.Properties["SessionId"].ToString()); 
     return result; 
    } 

    public async void ShowLoader(bool isVisible) 
    { 
     ActLoder.IsRunning = isVisible; 
     ActLoder.IsVisible = isVisible; 
     ActLoder.IsEnabled = true; 
    } 
} 

Répondre

0

GetCurrentDocuments renvoie une Task<DocumentsResponse> et vous n'êtes pas en attente pour activer cela Task.

var appointsData = await GetCurrentDocuments(); 

Mais, vous ne devriez pas await dans un .ctor.

Quelque chose comme ça vous aider à démarrer (je l'ai fait à la volée, donc corriger toute erreur fautes de frappe/syntaxe/etc:

public partial class CurrentDocuments : ContentPage 
{ 
    public CurrentDocuments() 
    { 
     InitializeComponent(); 
     new Task(Initializer).Start(); 
    } 

    public async void Initializer() 
    { 
     ShowLoader(true); 
     NavigationPage.SetBackButtonTitle(this, ""); 
     Title = "CURRENT DOCUMENTS"; 
     Task<DocumentsResponse> appointsData; 
     await Task.Run(async() => 
     { 
      appointsData = await GetCurrentDocuments(); 
     }).ContinueWith(_ => 
     { 
      Device.BeginInvokeOnMainThread(() => { 
       ListViewAppointments.ItemsSource = appointsData; 
       ShowLoader(false); 
      }); 
     }); 
    } 

    public void ShowLoader(bool isVisible) 
    { 
     ActLoder.IsRunning = isVisible; 
     ActLoder.IsVisible = isVisible; 
     ActLoder.IsEnabled = true; 
    } 

    public async Task<DocumentsResponse> GetCurrentDocuments() 
    { 
     DocumentManager manager = new DocumentManager(); 
     var result = await manager.GetCurrentDocuments(Application.Current.Properties["SessionId"].ToString()); 
     return result; 
    } 
} 
+0

Merci pour la réponse, je l'ai mis à jour mon code avec votre code et quelques changements mais je ne vois pas le chargeur .. la page montre juste avec toutes les données. – Mahajan344