在事件上更新Xamarin ContentPage内容不起作用

时间:2015-01-20 16:03:13

标签: xamarin xamarin.forms

可能是一个基本问题,但是如何强制页面更新内容(在已经加载之后)?我试图在网络请求后动态更新网页。事件即将发生,但似乎并没有让内容更新。

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,从内存中执行(不要在我面前有确切的代码)

1 个答案:

答案 0 :(得分:6)

你应该把它放在Device.BeginInvokeOnMainThread()里面,如下所示

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

    this.Content = layout;
  });
}