.NET 8 MAUI Windows打包后崩溃:跨线程调用接口异常
问题:MAUI Windows打包后启动崩溃,抛出跨线程接口调用异常
我们正在将Xamarin.Forms应用迁移为.NET 8 MAUI Windows应用,本地运行无构建错误,可正常启动至登录页面(暂未深入开发API相关功能);但打包后启动即崩溃,抛出异常The application called an interface that was marshalled for a different thread。
以下是我的App.cs代码:
public partial class App : Application { DataRepository _dataRepository; public App() { _dataRepository = new DataRepository(UIConstants.DatabasePath); try { MainPage = new LoadingPage(); } catch (WebException ex) { Task.Run(() => _dataRepository.SaveErrorLog(ex)); } catch (Exception ex) { Task.Run(() => _dataRepository.SaveErrorLog(ex)); MainPage = new NavigationPage(new ErrorPage(ex)); } } protected async override void OnStart() { // Handle when your app starts ValidateLogin(); //Clear PSOs left over in SQLLite when application starts var psos = _dataRepository.GetPSOs(); foreach (var pso in psos) { await _dataRepository.DeletePSO(pso); } // Debug.WriteLine("ONSTART"); } protected override void OnSleep() { // Handle when your app sleeps MainViewPage mainpage = Application.Current.MainPage as MainViewPage; if (mainpage != null) { Task.Run(() => mainpage.SaveBeforeExit()).Wait(); //Must dispose of dailylogpage. New one will be created on Resume mainpage.DisposeDailylogPage(); } //Debug.WriteLine("ONSLEEP"); } protected override void OnResume() { // Debug.WriteLine("ONRESUME"); ValidateLogin(); } private void UpdateLookups() { try { if (LookupViewModel.IssuedTos == null || LookupViewModel.IssuedTos.Count <= 0) { Task.Run(async () => { await SynchHelper.UpdateLookupsFromServer(); }); } } catch (Exception) { // fail will happen on expired tokens } } private void ValidateLogin() { //don't validate login if no network //TODO After update, add permission in droid project to access network state if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet) { if (!_dataRepository.CheckIfFirstTimeLogin()) { this.Dispatcher.Dispatch(() => { MainPage = new MainViewPage(); }); } else { //TODO Add a message to let user know no connection and no user in db to load offline. Need to login when connection is established return; } } Task.Factory.StartNew(async () => { try { if (!_dataRepository.CheckIfFirstTimeLogin()) { UserSecurityDA userSecurityDA = new UserSecurityDA(); SouthernUserInfo southernUser = await userSecurityDA.GetSouthernUserInfo(ISwitchSecurity.AuthBearerToken); this.Dispatcher.Dispatch(() => { MainPage = new MainViewPage(); }); } else { this.Dispatcher.Dispatch(() => { MainPage.Navigation.PushModalAsync(new LoginPage()); }); } } catch { this.Dispatcher.Dispatch(() => { MainPage.Navigation.PushModalAsync(new LoginPage(false, true)); }); } }); } }
已尝试将BeginInvokeOnMainThread替换为Dispatcher.Dispatch(),但问题仍未解决,恳请协助排查。
内容的提问来源于stack exchange,提问作者Zack Watson
相关产品推荐
相关产品推荐

