Reputation: 39
We are working on converting a Xamarin.Forms app into a MAUI windows app. No build errors and the application will run locally (at least to the login page. havent started on the API yet beyond it) But when we package it it will attempt to run and crash giving the following exception:
This is my 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: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/communication/networking?view=net-maui-8.0&tabs=android
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));
});
}
});
}
I attemped changing the BeginInvokeonMainThreads to Dispatcher.Dispatch() but that does not appear to have worked
Upvotes: 0
Views: 141
Reputation: 39
I found the problem: Someone had set the target entry point to the maui project directly. changing the target entry point to the variable $(targetentrypoint) worked.
Upvotes: 0
Reputation: 13899
You can only update the UI from the main UI thread. If you are running code on a different thread and need to update the UI, you can try to add your code in Main thread.
On Maui,for how to invoke on the main thread , you can check here:
Which is better MainThread.Being/Invoke... VS Dispatcher.Dispatch.
Upvotes: 0