Xamarin ContentPage content refresh on event not working

This may be the main question, but how to get the page to refresh its content (after it has already been loaded)? I am trying to dynamically refresh a page after a web request. The event fires, but it does not seem to refresh the content.

public StartPage : ContentPage
{
    public StartPage()
    {
        var layout = new StackLayout
        {
            Children = 
            {
                new Label { Text = "Preview Page" }
            }
        };

        this.Content = layout;
    }

    //this gets called from a web service call with the text to display
    public void Update(string text)
    {
        var layout = new StackLayout
        {
            Children = 
            {
                new Label { Text = text }
            }
        };

        this.Content = layout;

        //this fires, but nothing changes
        //how can I force the page to refresh??
    }
}

Aplogies for code, if there are errors doing this from memory (I don't have the exact code in front of me)

+1
source share
1 answer

You should put it inside Device.BeginInvokeOnMainThread()as below

public void Update(string text)
{
  Device.BeginInvokeOnMainThread(() =>
  {
    var layout = new StackLayout
    {
        Children = 
        {
            new Label { Text = text }
        }
    };

    this.Content = layout;
  });
}
+6
source

Source: https://habr.com/ru/post/1662667/


All Articles