ios- Navigation.PushModalAsync doesn't work sometimes

sweetssweets Member ✭✭

Hello, I am working on shared project.
I am trying to open a page automatically when some conditions meet. for that i use MessagingCenter. when I try to push a page using PushModalAsync, sometimes control doesn't come back to the next line written next to it.
Also I can't change it to PushAsync because my code is almost written and I am not able to change this to whole project(I am using MainPage as rootpage instead of navigation page.). This thing is fully functional in android. I have issue in iOS

Also, This thing doesn't happen everytime. sometimes intended page opens easily.

Attaching sample project here. It demonstrates the basic structure of my app and how I manage to open page via messaging center.

Thank you :smile:

Answers

  • JohnHardmanJohnHardman GBUniversity mod
    edited July 7

    @sweets said:
    sometimes control doesn't come back to the next line written next to it.

    I haven't looked at your code (I rarely open zip files referenced in the forums), so a generic answer coming up.

    Depending on quite what you mean by "sometimes control doesn't come back to the next line written next to it.", options include:
    (1) The call to PushModalAsync is not being made on the UI thread. Try using Device.BeginInvokeOnMainThread if this is a possibility.
    (2) The code instantiates a new Page (i.e. Navigation.PushModalAsync(new MyNewPage()); but the page's constructor does something that results in a deadlock
    (3) The code instantiates a new Page (i.e. Navigation.PushModalAsync(new MyNewPage()); but the page's constructor throws an exception, the knock-on effect of which is subsequently silently swallowed

  • sweetssweets Member ✭✭

    Thanks @JohnHardman for the comment. In the main code, I have already put it in Device.BeginInvokeOnMainThread. So point 2 and 3 could be possibilities. One more thing I forgot to add in the question is the page I push using Navigation.PushModalAsync is tabbed page. Is there any problem adding tabbed page inside navigation page ?

  • JohnHardmanJohnHardman GBUniversity mod

    @sweets said:
    Thanks @JohnHardman for the comment. In the main code, I have already put it in Device.BeginInvokeOnMainThread. So point 2 and 3 could be possibilities. One more thing I forgot to add in the question is the page I push using Navigation.PushModalAsync is tabbed page. Is there any problem adding tabbed page inside navigation page ?

    I don't use tabbed pages, so I'll have to leave that to somebody else to answer.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @sweets I noticed you placed a send method in your tabbed page's constructor. This will cause opening a blank page on iOS. However, Android won't respond to this at all and shows tab1 page as default.
    If you are meaning this issue try to move it to the OnAppearing:

    bool firstLoading = true;
    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        if (firstLoading)
        {
            firstLoading = false;
            MessagingCenter.Send<App, string>((App)Application.Current, "ShowTableFromActiveList", "string");                
        }            
    }
    

    I made a filed here to make sure this only triggers at first time this tabbed page is initializing.

  • sweetssweets Member ✭✭

    Hi @LandLu Thanks for the comment. It is not the issue actually. I made it ins sample only. actual message is sent from attached ViewModel. What happens here is first I get the command from server when the app starts. If I don't get command then the page will be opened from tabbed page.

    To open page automatically I do this :smile:

    I send one message and subscribe it in MainLobby.cs. Then after subscribe I call method from attached viewmodel. In that method of viewmodel, I send the above message MessagingCenter.Send<App, string>((App)Application.Current, "ShowTableFromActiveList", "string");. I wrote it in page for example purpose. I thought it would be sufficient. Sorry for taking your time. But please help me if possible. I have been searching for the answer since last week. :neutral:

  • LandLuLandLu Member, Xamarin Team Xamurai

    @sweets If it is not what you want please adjust your sample and attach it here again. I only saw you sent the message in your MainLobby's constructor.
    I'm confused about what you want. Please specify more details.

  • sweetssweets Member ✭✭

    @LandLu , Thank you for your response.
    Currently Not possible for me to update my sample right now but I can write code here.

    App.Xaml.cs

    `public partial class App : Application
    {
    public static MainUserViewModel objMainUserViewModel;
    public App()
    {
    InitializeComponent();

            objMainUserViewModel = new MainUserViewModel();
    
            MainPage = new MainPage();
        }
    

    }`

    `public class MainUserViewModel
    {
    public MainUserViewModel()
    {
    }

        public void GetCommand()
        {
            // GET COMMAND FROM SERVER AFTER THAT CALL MESSAGING CENTER
            MessagingCenter.Send<App, string>((App)Application.Current, "OPENTABLE", "MainUserViewModel");
        }
    }`
    

    In the MainPage in sample
    private void Button_Clicked(object sender, EventArgs e) { Application.Current.MainPage = new MainLobby(); }

    MainLobby.cs

    `public class MainLobby : TabbedPage
    {
    private CommonViewModel vm = new CommonViewModel();
    public MainLobby()
    {
    Children.Add(new Tab1() { Title = "Tab1" });
    Children.Add(new Tab2() { Title = "Tab2" });

            AddMessages();
    
            App.objMainUserViewModel.GetCommand();
        }
    
        void AddMessages()
        {
            MessagingCenter.Subscribe<App, string>((App)Application.Current, "OPENTABLE", (sender, args) =>
            {
                OpenTable(args);
            });
        }
    
        private void OpenTable(string args)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                vm.GetRunningData();
            });
        }
    

    }`

    `class CommonViewModel
    {
    public CommonViewModel()
    {
    }

        public Task GetRunningData()
        {
            try
            {
                // GET DATA FROM API CALL                 
                MessagingCenter.Send<App, string>((App)Application.Current, "ShowTableFromActiveList", "CommonViewModel");
                return Task.CompletedTask;  // I DO NOT RETURN  THIS IN MAIN PROJECT BECAUSE THERE IS AWAITED CALL TO WEBAPI            
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }`
    

    ShowTableFromActiveList is subscribed in Tab1 constructor. One more thing I forgot to include in question that in the line

    await Navigation.PushModalAsync(new NavigationPage(new Table())); Table is a TabbedPage. when user selects an item from Tab1 listview specific tabel will open. I can have multiple tables open at a time. so I took TabbedPage. Currently I want that automatically open a table on server request. manually it works but automatically it doesn't.

    Hope you understand this :smile:

    Thanks.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @sweets As I said above, App.objMainUserViewModel.GetCommand(); should be placed in the OnAppearing event instead of the constructor to show a new page.
    I moved the push code from tab1 to your MainLobby tabbed page because it seems more appropriate.
    Here is my sample you can test it on your side.

  • sweetssweets Member ✭✭

    @LandLu Thank for taking time to help me. I will try and let you know :smile:

Sign In or Register to comment.