如何解决线程问题在块
问题描述:
当我试图改变如何解决线程问题在块
GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<status>(this, async Status=>
{
this.txt1.text = "Hi"
});
我对如何处理这个错误
答
你可真糊涂变量的值只从主UI线程更新UI。为此,您需要使用Device.BeginInvokeOnMainThread方法在UI线程上调用UI的更新。像这样:
GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<status>(this, async Status=>
{
Device.BeginInvokeOnMainThread(() =>
{
this.txt1.text = "Hi";
});
});
每个平台都有它自己的本地方法来调用UI线程。在WinPhone中:
GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<status>(this, async Status=>
{
var coreWindow = CoreWindow.GetForCurrentThread();
// Dispatcher needed to run on UI Thread
dispatcher = coreWindow.Dispatcher;
// RunAsync all of the UI info.
dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
this.txt1.text = "Hi";
});
});
谢谢..但我使用xamarin原生 –
@ElstineP我刚刚更新了我的答案Xamarin.winphone –