You can try to put the code into the OnStart method instead of the constructor. Such as:
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override void OnStart()
{
new Action(async () => await firebaseExists())();
if(MainPage == null)
{
if (firebaseDocExists == false) MainPage = new MyPage();
else MainPage = new NavigationPage(new MainPage());
}
}
bool firebaseDocExists = false;
private async Task DocExists()
{
var firebaseDoc = await CrossCloudFirestore.Current
.Instance
.Collection("myCollection")
.Document("docId")
.GetAsync();
firebaseDocExists = firebaseDoc.Exists;
}
}
Update 1:
I can't understand why you used new Action(async () => await firebaseExists())();
, but you can try to change the code such as:
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override async void OnStart()
{
bool firebaseDocExists = await DocExists();
if(MainPage == null)
{
if (firebaseDocExists == false) MainPage = new MyPage();
else MainPage = new NavigationPage(new MainPage());
}
}
private async Task<bool> DocExists()
{
var firebaseDoc = await CrossCloudFirestore.Current
.Instance
.Collection("myCollection")
.Document("docId")
.GetAsync();
return firebaseDoc.Exists;
}
}
Update 2:
Set the default page as NavigationPage(new MainPage());
in the constructor and change it in the OnStart method.
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
protected override async void OnStart()
{
bool firebaseDocExists = await DocExists();
if (firebaseDocExists == false) MainPage = new MyPage();
}
private async Task<bool> DocExists()
{
var firebaseDoc = await CrossCloudFirestore.Current
.Instance
.Collection("myCollection")
.Document("docId")
.GetAsync();
return firebaseDoc.Exists;
}
}
Update 3:
I found that MainThread.BeginInvokeOnMainThread
can set the MainPage as as NavigationPage(new MainPage())
in the OnStart method. So you can also used the following code:
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override async void OnStart()
{
bool firebaseDocExists = await DocExists();
if(MainPage == null)
{
MainThread.BeginInvokeOnMainThread(()=>
{
if (firebaseDocExists == false) MainPage = new MyPage();
else MainPage = new NavigationPage(new MainPage());
});
}
}
private async Task<bool> DocExists()
{
var firebaseDoc = await CrossCloudFirestore.Current
.Instance
.Collection("myCollection")
.Document("docId")
.GetAsync();
return firebaseDoc.Exists;
}
}
Update 4:
protected override async void OnStart()
{
bool firebaseDocExists = await DocExists();
if(MainPage == null)
{
if (firebaseDocExists == false) MainPage = new MyPage();
else
{
MainThread.BeginInvokeOnMainThread(()=>
{
MainPage = new NavigationPage(new MainPage());
});
}
}
}
Update 5:
public partial class App : Application
{
public App()
{
InitializeComponent();
DocExists().Wait();
if (firebaseDocExists == false) MainPage = new MyPage();
else MainPage = new NavigationPage(new MainPage());
}
bool firebaseDocExists = false;
private async Task DocExists()
{
var firebaseDoc = await CrossCloudFirestore.Current
.Instance
.Collection("myCollection")
.Document("docId")
.GetAsync();
firebaseDocExists = firebaseDoc.Exists;
}
}