Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to bring up the native iOS and Android busy indicators with Xamarin Forms apps?

I have a Forms app that takes a few seconds to populate data when I click on a viewCell.

Is there a way that I can show a circular busy indicator during this time through custom renderers or something like that?

like image 502
Alan2 Avatar asked Sep 29 '17 14:09

Alan2


People also ask

How is Xamarin used for native Android and iOS development?

Xamarin Forms enable fast prototyping with built-in UI libraries, adapted both to Android and iOS; Rich access to native APIs – just like in native apps, you can get access to the user's Bluetooth, camera, microphone. Features that use these API can be written in a specific language for faster performance.

Does Xamarin work with iOS and Android?

Android. Android application development requires the Java and Android SDKs to be installed. The SDKs provide the compiler, emulator, and other tools required for building, deployment, and testing. Java, Google's Android SDK and Xamarin's tools can all be installed and run on Windows and macOS.

What is activity indicator in Xamarin forms?

The Xamarin. Forms ActivityIndicator control displays an animation to show that the application is engaged in a lengthy activity. Unlike the ProgressBar , the ActivityIndicator gives no indication of progress. The ActivityIndicator inherits from View .

Is Xamarin better than native?

Though both Xamarin and React Native offer near-native performance, Xamarin runs the fastest code on Android and iOS and has a user interface (UI) for using native tools. TLDR: In Xamarin vs. React Native, Xamarin has more brownie points for native-like performance. Xamarin wins.


2 Answers

You can implement the same by using ActivityIndicator control.

If you are expecting have busy-indicators on multiple pages, then would recommend to implement this using the ControlTemplate (it also allows you to define overlays if needed).

Page Template

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

Sample usage:

XAML - assign template to page

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            ControlTemplate="{StaticResource DefaultTemplate}"
            .. >
    .... 
</ContentPage>

View Model

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

enter image description here

like image 143
Sharada Gururaj Avatar answered Sep 30 '22 19:09

Sharada Gururaj


You can use Acr.UserDialogs, it's a cross-platform package with busy indicators, dialogs, toasts, etc.

In your case, you need to use Loading.

using (Acr.UserDialogs.UserDialogs.Instance.Loading("your message here"))
{                
    //your long task here
}

For example...

enter image description here

like image 32
Jesus Angulo Avatar answered Sep 30 '22 19:09

Jesus Angulo