本文介绍了无法更新事件的Xamarin ContentPage内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能是一个基本问题,但是如何强制页面更新它的内容(在它已经被加载之后)?我正在尝试在Web请求后动态更新页面.该事件将触发,但似乎并没有刷新内容.
Might be a basic question, but how do you force a page to update it's content (after it's already been loaded)? I'm trying to dynamically update a page after a web request. The event fires, but it doesn't seem to be refreshing 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 the code if there are errors, doing it from memory (don't have the exact code in front of me)
推荐答案
您应将其放入Device.BeginInvokeOnMainThread()
内,如下所示
You should put it inside Device.BeginInvokeOnMainThread()
like below
public void Update(string text)
{
Device.BeginInvokeOnMainThread(() =>
{
var layout = new StackLayout
{
Children =
{
new Label { Text = text }
}
};
this.Content = layout;
});
}
这篇关于无法更新事件的Xamarin ContentPage内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!