【发布时间】:2020-08-24 13:47:56
【问题描述】:
我将 MVVMCross 与我的跨平台 Windows Phone 和 Android 应用程序一起使用。在核心项目的主视图模型中,我正在使用 TPL 做一些后台工作,我想确保在回调中,当我对将触发 UI 更改的视图模型的属性进行更改时,代码运行在UI线程,我该如何实现?
对于代码,这是它喜欢的方式
private MvxGeoLocation _currentLocation;
private Task<MvxGeoLocation> GetCurrentLocation()
{
return Task.Factory.StartNew(() =>
{
while (_currentLocation == null && !LocationRetrievalFailed)
{
}
return _currentLocation;
});
}
var location = await GetCurrentLocation();
if (LocationRetrievalFailed)
{
if (location == null)
{
ReverseGeocodingRequestFailed = true;
return;
}
// Show toast saying that we are using the last known location
}
Address = await GooglePlaceApiClient.ReverseGeocoding(location);
【问题讨论】: