Xamarin Forms Grid IsVisbile binding only works in one way

I noticed a strange behavior with my XAML page. This is my PageContent. I’m running the app in the iOS Simulator.

<Grid>
<Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid IsVisible="{Binding IsBusy, Converter={StaticResource BooleanReverseVisibilityConverter}}">
            <!-- Main content -->
        </Grid>

        <Grid IsVisible="{Binding IsBusy}">
            <!-- Busy indicator -->
            <Label Text="Doing some stuff..."/>
        </Grid>
    </Grid>

I have my main content in the upper grid. While page is doing stuff, the main grid should be invisible and the busy grid schould become visible.

For know, only the main content becomes invisible, however the busy grid stays invisible as well.

This is my binding property.

private bool _isBusy = false;
        public bool IsBusy
        {
            get => _isBusy;
            set
            {
                if (_isBusy == value) return;
                _isBusy = value;
                OnPropertyChanged();
            }
        }

I do set the IsBusy variable in an AsyncCommand (also tried a normal Command). The example is shown below.

Command

CalculateCommand = new AsyncCommand(async () => await CalculateAction(), (t) => 
CalculateCommand_CanExcecute(t));

Task

private async Task CalculateAction()
        {
            IsBusy= true;
            try
            {
                IncrementalNumber++;
                //Do some calculation

                await ShellNavigationManager.GoToAsync(string.Format("{0}?cid={1}", ShellRoute.CalculationResultPage.ToString(), culc.Id), false);
                IsCalculating = false;

            }
            catch (Exception exc)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Strings.DialogUnexpectedErrorOccurredHeadline,
                    string.Format(Strings.DialogUnexpectedErrorOccurredFormatedContent, exc.Message),
                    Strings.Close
                    );

                IsBusy= false;

            }
        }

I guess it has something to do with the thread. I tried to invoke the IsBusy property in the main thread.

Device.BeginInvokeOnMainThread(async () =>
                {
                    {
                        IsBusy = true;
                    }
                });

This seems to work, however this takes a while. The calculation is almost done until the invoke sets the visual UI to the current state. I’m kind of lost here. Thanks!

Answer

I figured it out. I seemed to be a bug in Visual Studio. I just cut (ctrl + x) and paste the whole XAML code again. Since that, it’s working as expected. No code changes.. strange, but glad it’s working now.

Leave a Reply

Your email address will not be published. Required fields are marked *