【问题标题】:Xamarin form activityindicator not working in constructorXamarin 形成活动指示器在构造函数中不起作用
【发布时间】:2017-09-28 07:10:54
【问题描述】:

我正在处理 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;
    }
}

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.forms


    【解决方案1】:

    GetCurrentDocuments 正在返回一个 Task&lt;DocumentsResponse&gt;,而您并没有等待它激活该 Task。

    var appointsData = await GetCurrentDocuments();
    

    但是,您不应该在.ctor 中使用await。

    这样的事情会让你开始(我是即时完成的,所以更正任何拼写错误/语法错误/等等:

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

    【讨论】:

    • 感谢您的回复,我已经用您的代码和一些更改更新了我的代码,但我没有看到 loader.. 页面只显示所有数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多