connersz connersz - 27 days ago 19
C# Question

Xamarin Stacklayout not being populated

I have a Stacklayout on screen and it's populated at different times with labels from the code-behind.

The problem is, I have this one method that doesn't populate the Stacklayout. It uses the same identical code as the other methods. It runs through the code successfully but it just does not show on screen.

'readout' is the Stacklayout.

public async void HandleNFC(string convertedtag)
{
PeopleLocationsForUserRoot peoplelocationforuser = await WebDataAccess.GetPeopleLocationForUser("ConroyJ", 1);

Label label1 = new Label { Text = "Successfully clocked out @ " + convertedtag, TextColor = Color.Black };

readOut.Children.Add(label1);
DisplayAlert("NFC", convertedtag, "cancel");

}

Answer

in a Forms app, you can get a reference to your MainPage like this

  App.Current.MainPage

you will also need to cast it correctly in order to call a custom method

  var main = (MyMainPageType) App.Current.MainPage;
  main.HandleNFC(data);