Xamarin表单activityindicator不在构造函数中工作

时间:2017-09-28 07:10:55

标签: xamarin xamarin.ios xamarin.forms

我正在处理Xamarin表单,我需要在表单加载时显示数据库中的数据。 所以我想在数据库操作需要时间时显示活动指示器。

我在构造函数加载时将ActivityIndi​​cator设置为true,并在结束时将其设置为false。

但它没有显示

这是代码

Xaml如下

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

和背后的代码

 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 个答案:

答案 0 :(得分:0)

GetCurrentDocuments正在返回Task<DocumentsResponse>,您无法等待它激活Task

var appointsData = await GetCurrentDocuments();

但是await中不应该.ctor

像这样的东西会让你开始(我在飞行中做了这个,所以纠正任何拼写错误/语法错误等等:

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