locked
Xamarin.Forms Error when navigate to another page in button_clicked RRS feed

  • Question

  • User394552 posted

    Hi . In my Xamarin.Forms project I have a SplashScreen for Android and an AppDelegate for IOS that check some conditions and then pass some data to App.xaml.cs . Then

    ``` public App(string isLogin) { InitializeComponent();

            if (isLogin == "Nologin")
                App.Current.MainPage = new LoginPage();
            else
            {
                switch (isLogin)
                {
                    case "Update": 
                            App.Current.MainPage = new MessagePage("Some Message");
                        break;
    
                    case "Internet":
                        App.Current.MainPage = new MessagePage("Some Message");
                        break;
    
                    default:
                        App.Current.MainPage = new MainPage(isLogin);
                        break;
                }
            }
    
    
        }
    

    ```

    Now in the MessagePage I have this

    ``` private async void RefreshButton_Clicked(object sender, EventArgs e) { string result = await AppStart.DoStartAsync().ConfigureAwait(false);

        if (result == "Login")
           await Navigation.PushAsync(new LoginPage());
    

    } ```

    that show this error :

    System.InvalidOperationException: 'PushAsync is not supported globally on Android, please use a NavigationPage.'

    and if I use this

    if (result == "Login") Application.Current.MainPage = new NavigationPage(new LoginPage());

    Show this error :

    Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.'

    What can I do ?

    Tuesday, May 12, 2020 5:13 AM

Answers

  • User371688 posted

    You can just wrap the page with NavigationPage in class App.xaml.cs ,for example:

       public App(string isLogin)
        {
            InitializeComponent();
    
            // MainPage = new MainPage();
    
            if (isLogin == "Nologin")
                //  App.Current.MainPage = new LoginPage();
    
                App.Current.MainPage = new NavigationPage( new LoginPage());
            else
            {
                switch (isLogin)
                {
                    case "Update":
                        // App.Current.MainPage = new MessagePage("Some Message");
                        App.Current.MainPage = new NavigationPage(new MessagePage("Some Message"));
                        break;
    
                    case "Internet":
                        // App.Current.MainPage = new MessagePage("Some Message");
                        App.Current.MainPage = new NavigationPage(new MessagePage("Some Message"));
                        break;
    
                    default:
                        //App.Current.MainPage = new MainPage(isLogin);
    
                        App.Current.MainPage = new NavigationPage(new MainPage(isLogin));
                        break;
                }
            }
    

    Meanwhile,in MessagePage use code like this:

        private async  void RefreshButton_Clicked(object sender, EventArgs e)
        {
            if (result.Equals("Some Message"))
                await Navigation.PushAsync(new LoginPage());
        }
    

    Note: If you want to compare two string, please use function Equals

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 12, 2020 6:32 AM
  • User394552 posted

    @jezh

    this code solved my problem :

    Device.BeginInvokeOnMainThread(async () => { await Navigation.PushAsync(new LoginPage()); });

    Of course I changed my App.xaml.cs to this as @jezh said

    App.Current.MainPage = new NavigationPage( new LoginPage());

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 12, 2020 8:42 AM

All replies

  • User371688 posted

    You can just wrap the page with NavigationPage in class App.xaml.cs ,for example:

       public App(string isLogin)
        {
            InitializeComponent();
    
            // MainPage = new MainPage();
    
            if (isLogin == "Nologin")
                //  App.Current.MainPage = new LoginPage();
    
                App.Current.MainPage = new NavigationPage( new LoginPage());
            else
            {
                switch (isLogin)
                {
                    case "Update":
                        // App.Current.MainPage = new MessagePage("Some Message");
                        App.Current.MainPage = new NavigationPage(new MessagePage("Some Message"));
                        break;
    
                    case "Internet":
                        // App.Current.MainPage = new MessagePage("Some Message");
                        App.Current.MainPage = new NavigationPage(new MessagePage("Some Message"));
                        break;
    
                    default:
                        //App.Current.MainPage = new MainPage(isLogin);
    
                        App.Current.MainPage = new NavigationPage(new MainPage(isLogin));
                        break;
                }
            }
    

    Meanwhile,in MessagePage use code like this:

        private async  void RefreshButton_Clicked(object sender, EventArgs e)
        {
            if (result.Equals("Some Message"))
                await Navigation.PushAsync(new LoginPage());
        }
    

    Note: If you want to compare two string, please use function Equals

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 12, 2020 6:32 AM
  • User394552 posted

    Thank you for your answer

    I changed the code exactly s you say but show this error again

    Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.'

    Tuesday, May 12, 2020 7:24 AM
  • User394552 posted

    @jezh

    Thank you for your answer

    I changed the code exactly as you say but show this error again

    Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.'

    Tuesday, May 12, 2020 7:29 AM
  • User371688 posted

    I couldn't reproduce this question ,could you please share a basic demo so that we can try to test on our side?

    Tuesday, May 12, 2020 7:48 AM
  • User394552 posted

    @jezh

    thnak you for your time

    My project is attached .

    Tuesday, May 12, 2020 8:24 AM
  • User394552 posted

    @jezh

    this code solved my problem :

    Device.BeginInvokeOnMainThread(async () => { await Navigation.PushAsync(new LoginPage()); });

    Of course I changed my App.xaml.cs to this as @jezh said

    App.Current.MainPage = new NavigationPage( new LoginPage());

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 12, 2020 8:42 AM
  • User371688 posted

    Congrats, could you please mark the useful reply to finish this topic? Thanks. :)

    Tuesday, May 12, 2020 9:31 AM