question

RedBeans-4480 avatar image
0 Votes"
RedBeans-4480 asked LeonLu-MSFT commented

How to create a singleton Page for Navigation Page(Xamarin Forms)

I am using Android Foreground service on my App.
When I kill the app and restart it, my MainPage is created again.

 await Navigation.PushAsync(new MainPage(), true);

I want to only create one MainPage and use it.

I tried the following code but did not work.

 public partial class LoginPage : ContentPage
 {
     static Page instance;
             :
     private async void Login(string id, string pass)
     {
                 :
         try
         {
                 :
             if (instance == null)
             {
                 instance = new MainPage();
                 Console.WriteLine("create MainPage");
             }
             else
             {
                 Console.WriteLine("MainPage is already created");
             }
             await Navigation.PushAsync(instance, true);
         }
         catch (Exception e)
         {
             await DisplayAlert("error", "unexpected error occured." + e.Message, "OK");
         }
         finally
         {
         }
             :


It shows
error
unexpected error occured. Page must not already have a parent.

dotnet-xamarinforms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Xamarin forms do not have singleton Page, but you can push a activity with LaunchMode.SingleTop.

[Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop)] public class MainScreen : AppCompatActivity
0 Votes 0 ·

0 Answers