有没有办法用Xamarin Forms应用程序启动原生iOS和Android繁忙指标?

Ala*_*an2 9 xamarin xamarin.forms

我有一个Forms应用程序,当我点击viewCell时需要几秒钟来填充数据.

有没有办法在此期间通过自定义渲染器或类似的东西显示循环繁忙指示器?

Sha*_*raj 3

您可以使用ActivityIndi​​cator控件来实现相同的功能。

如果您期望在多个页面上有繁忙指示器,那么建议使用ControlTemplate(它还允许您根据需要定义覆盖)来实现这一点。

页面模板

<Application.Resources>
    <ResourceDictionary>
        <ControlTemplate x:Key="DefaultTemplate">
            <Grid> 
                         <!-- page content -->
                <ContentPresenter />
                         <!-- overlay -->
                <BoxView BackgroundColor="Black" Opacity="0.5" 
                        IsVisible="{TemplateBinding BindingContext.IsBusy}"/>
                        <!-- busy indicator with text --> 
                <Frame HorizontalOptions="Center" VerticalOptions="Center"
                     IsVisible="{TemplateBinding BindingContext.IsBusy}">
                    <StackLayout>
                        <ActivityIndicator IsRunning="{TemplateBinding BindingContext.IsBusy}" />
                        <Label Text="{TemplateBinding BindingContext.BusyText}" />
                    </StackLayout>
                </Frame>
            </Grid>
        </ControlTemplate>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

使用示例:

XAML - 将模板分配给页面

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            ControlTemplate="{StaticResource DefaultTemplate}"
            .. >
    .... 
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

查看模型

public class BaseViewModel : ObservableObject
{
    bool _isBusy;
    public bool IsBusy
    {
        get => _isBusy;
        set => SetProperty(ref _isBusy, value);
    }

    string _busyText = "loading..";
    public string BusyText
    {
        get => _busyText;
        set => SetProperty(ref _busyText, value);
    }
}

public class TestViewModel : BaseViewModel
{
    public ICommand OnTapCommand {
        get => new Command(async (obj) =>
        {
            IsBusy = true;

            //do heavy lifting here
            await Task.Delay(2000);

            IsBusy = false;
        });
    }
...
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述