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">
    <ScrollView>
      <StackLayout x:Name="stackLayout" Padding="15">
      </StackLayout>
    </ScrollView>
    <StackLayout AbsoluteLayout.LayoutFlags="All"
                           AbsoluteLayout.LayoutBounds="0,0,1,1"
                           x:Name="actIndicatorContainer">
      <ActivityIndicator x:Name="actIndicator"
                         VerticalOptions="Center"
                         HorizontalOptions="Center" />
    </StackLayout>
  </AbsoluteLayout>

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(){
    InitializeComponent();
    BindingContext = _viewModel;
    actIndicator.IsRunning = true;
    actIndicator.IsVisible = true;

    Task.Run(() =>
    {
           // Some stuff that creates my grid including the controls - long running task
           SetupView(ActiveGrid);
           Device.BeginInvokeOnMainThread(() =>
           {
                 // Assign the grid to my stackLayout and deactivate the indicator
                 DefaultGrid = ActiveGrid;
                 stackLayout.Children.Add(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

Answers

Sign In or Register to comment.