0
votes

I am working on Xamarin forms,I need to display data from database on form load. So I want to display Activity indicator when database operation is taking time.

I am setting ActivityIndicator to true on constructor load andat the end setting it to false.

But its not showing

Here is code

Xaml is as below

<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>

and code behind

 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;
    }
}
1

1 Answers

0
votes

GetCurrentDocuments is returning a Task<DocumentsResponse> and you are not awaiting it to activate that Task.

var appointsData = await GetCurrentDocuments();

But, you should not await in an .ctor.

Something like this will get you started (I did this on the fly, so correct any typos/syntax errors/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;
    }
}