C# Xamarin表单:未从事件更新UI
我在更新ui时遇到一些问题。代码如下:C# Xamarin表单:未从事件更新UI,c#,multithreading,xamarin,asynchronous,xamarin.forms,C#,Multithreading,Xamarin,Asynchronous,Xamarin.forms,我在更新ui时遇到一些问题。代码如下: async Task StartListening() { try { if (CrossGeolocator.Current.IsListening) return; await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromMilliseconds(500), _
async Task StartListening()
{
try
{
if (CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromMilliseconds(500), _locationTrackingFrequency, true, new ListenerSettings
{
ActivityType = ActivityType.AutomotiveNavigation,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = false,
PauseLocationUpdatesAutomatically = false
});
CrossGeolocator.Current.PositionChanged += CrossGeolocator_Current_PositionChanged;
}
catch (Exception exception)
{
await LogEvents.LogEvent(exception.ToString(), 15);
}
}
当我按下一个按钮时,就会执行StartListening方法。
当gps位置更改时,将执行CrossGeolocator\u Current\u PositionChanged
public async void CrossGeolocator_Current_PositionChanged(object sender, PositionEventArgs e)
{
Device.BeginInvokeOnMainThread(() => { IsBusy = true; });
}
当我仍在该页面上时,Ui会更新,因此这意味着上述实现可以正常工作。但是,当我更改页面并且再次访问此页面时,即使执行了命令,ui也不会更新(我可以从调试中看到它)。
我所做的导航是查看模型到视图模型
private void RedirectToPage(MainMenuItem menuItem)
{
Device.BeginInvokeOnMainThread(async() =>
{
var type = menuItem?.ViewModelToLoad;
await _navigationService.NavigateToAsync(type);
});
}
有人知道为什么会发生这种情况吗。您能告诉我们在UI中进行更改的代码吗?Device.beginInvokeMainThread(()=>{IsBusy=true;});IsBusy是从视图绑定的属性。它还有onpropertychanged事件。public bool-IsBusy{get=>\u-IsBusy;set{u-IsBusy=value;OnPropertyChanged(nameof(IsBusy));}}您是否能够在iOS和Android、设备和模拟器上看到此问题?您还说“即使命令已执行”,您在谈论哪个命令?@Saamer Device.BeginInvokeOnMainThread(()=>{IsBusy=true;});这是命令。我已经在每个平台上测试了模拟器和设备。您可以放置一些
控制台。编写语句并检查调用的行吗?