Xamarin ActivityIndicator while setting page up

DevJDevJ CHMember ✭✭

I have a page which takes a few seconds to set up all the controls. To provide some information what's going on, I'd like to show an ActivityIndicator to the user.

  <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
      <StackLayout x:Name="stackLayout" Padding="15">
    <StackLayout AbsoluteLayout.LayoutFlags="All"
      <ActivityIndicator x:Name="actIndicator"
                         HorizontalOptions="Center" />

The idea is, to active the indicator, do some async stuff which creates a grid and then assign the grid to my stackLayout.

internal XamTrapGrid ActiveGrid = new XamTrapGrid();
internal XamTrapGrid DefaultGrid;

public void SetupPage(){
    BindingContext = _viewModel;
    actIndicator.IsRunning = true;
    actIndicator.IsVisible = true;

    Task.Run(() =>
           // Some stuff that creates my grid including the controls - long running task
           Device.BeginInvokeOnMainThread(() =>
                 // Assign the grid to my stackLayout and deactivate the indicator
                 DefaultGrid = ActiveGrid;
                 actIndicator.IsRunning = false;
                 actIndicator.IsVisible = false;


As soon as I try to assign the ActiveGrid to the stackLayout, I get a NullReferenceException. I can't see any null object in the debugger. What's wrong with this?

Best Answer


Sign In or Register to comment.