如何使用活动指示器,直到ListView加载Xamarin表单 [英] how to work with activity indicator until listview loaded xamarin form

查看:73
本文介绍了如何使用活动指示器,直到ListView加载Xamarin表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Web服务绑定listview,现在listview需要一些时间才能加载,具体取决于Internet连接,我需要activity indicator保持运行直到加载listview. 这是我的代码

xaml

<ListView x:Name="ReportListView" ItemsSource="{Binding ReportList}"
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Padding="9" HorizontalOptions="StartAndExpand" Orientation="Vertical" BackgroundColor="Lavender">
                    <Label Text="{Binding ReportName}" FontSize="Medium" WidthRequest="10000" TextColor="Blue" Style="{DynamicResource ListItemTextStyle}" />
                </StackLayout
            </ViewCell>
        </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

<ActivityIndicator HorizontalOptions="Center" VerticalOptions="Center" IsRunning="False" IsVisible="False" x:Name="activity"/>

我正在这样工作,这绝对不正确

.cs

activity.IsVisible = true;
activity.IsRunning = true;
wait.IsVisible = true;
progressControl.IsVisible = true;
ReportListView.IsVisible = false;
Task.Delay(5000);
BindingContext = new MainViewModel();

activity.IsVisible = false;
activity.IsRunning = false;
wait.IsVisible = false;
progressControl.IsVisible = false;

ReportListView.IsVisible = true;

带有此代码activity indicator

运行5秒钟,然后消失,listview的负载取决于网络连接. 在listview加载之前,活动指示器持续运行的地方我该如何工作?

解决方案

一种常见的方法是将ActivityIndicatorIsVisible属性绑定到视图模型的某些IsBusyIsLoading属性. >

XAML:

<ListView x:Name="ReportListView" ItemsSource="{Binding ReportList}">
    ...
</ListView>
<ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}" />

ViewModel:

bool isBusy;
public bool IsBusy
{
    get => isBusy; 
    set 
    { 
        isBusy = value; 
        OnPropertyChanged(); 
    }
}

async Task LoadItemsAsync()
{
    try
    {
        IsBusy = true;

        // Call your web service here
        var items = await service.LoadSomthingAsync();
    }
    catch (Exception ex)
    {
        // Handle exception
    }
    finally
    {
        IsBusy = false;
    }
}

在这种情况下,每次将IsBusy设置为true时,应用程序都会显示一个加载指示器.

I am binding listview from web service, now some time listview take time to load depend on internet connection, i need activity indicator keep running until listview is load. here is my code

xaml

<ListView x:Name="ReportListView" ItemsSource="{Binding ReportList}"
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Padding="9" HorizontalOptions="StartAndExpand" Orientation="Vertical" BackgroundColor="Lavender">
                    <Label Text="{Binding ReportName}" FontSize="Medium" WidthRequest="10000" TextColor="Blue" Style="{DynamicResource ListItemTextStyle}" />
                </StackLayout
            </ViewCell>
        </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

<ActivityIndicator HorizontalOptions="Center" VerticalOptions="Center" IsRunning="False" IsVisible="False" x:Name="activity"/>

i am working like this which is absolutely not correct

.cs

activity.IsVisible = true;
activity.IsRunning = true;
wait.IsVisible = true;
progressControl.IsVisible = true;
ReportListView.IsVisible = false;
Task.Delay(5000);
BindingContext = new MainViewModel();

activity.IsVisible = false;
activity.IsRunning = false;
wait.IsVisible = false;
progressControl.IsVisible = false;

ReportListView.IsVisible = true;

with this code activity indicator run for 5 sec and disappear and listview load depends on network connection. How can i work where activity indicator keep running until listview load?

解决方案

A common approach is to bind the IsVisible property of the ActivityIndicator to some IsBusy or IsLoading property of your view model.

XAML:

<ListView x:Name="ReportListView" ItemsSource="{Binding ReportList}">
    ...
</ListView>
<ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}" />

ViewModel:

bool isBusy;
public bool IsBusy
{
    get => isBusy; 
    set 
    { 
        isBusy = value; 
        OnPropertyChanged(); 
    }
}

async Task LoadItemsAsync()
{
    try
    {
        IsBusy = true;

        // Call your web service here
        var items = await service.LoadSomthingAsync();
    }
    catch (Exception ex)
    {
        // Handle exception
    }
    finally
    {
        IsBusy = false;
    }
}

In this case, each time when you set IsBusy to true, the app will show a loading indicator.

这篇关于如何使用活动指示器,直到ListView加载Xamarin表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆