Call back to parent view from modal

LukeConnerLukeConner USMember, University ✭✭

So I am trying to pop up a login window, then once they log in call back to the parent view and dismiss the modal. This is what I'm trying
Is is the main parent window
async void PatientButton_Clicked (object sender, EventArgs e)
{
await Navigation.PushModalAsync (new Login ());

        }

        public async void ReturnAndDismissLogin(User u)
        {
            this.View.User = u;
            await Navigation.PopModalAsync();
        }

And for simplicity's sake, here is my cancel button on the login view

    void CancelButton_Clicked (object sender, EventArgs e)
    {
        var home = (Home)this.ParentView;
        home.ReturnAndDismissLogin(new User());
    }

So when I try to reference this.Parent or this.ParentView they are null. How would I access the parent view? Or am I going about this the wrong way?

Best Answers

Answers

  • TektonTekton USMember ✭✭✭
    edited August 2015

    There are a number of approaches you could take differently. The way I've been handling login looks like this in App.cs:

    //Perhaps considered bad practice?
    //Helps lets me navigate from anywhere I'd like easily. Such as App.MainNavigationPage.PushAsync (new SomePage());
    public static MyNavigationPage MainNavigationPage;
    
    //...
    
    public App ()
    {
        //InitEnvironment ();
    
        //InitUser ();
    
        //JsonConvert.DefaultSettings = () => new JsonSerializerSettings { 
        //  NullValueHandling = NullValueHandling.Ignore
        //};
    
        MainPage = GetMainPage ();
    }
    
    public Page GetMainPage ()
    {
        Page currentPage;
    
        if (IsAuthenticated == true) {
            currentPage = new Dashboard ();
        } else {
            currentPage = new LoginPage ();
        }
    
        MainNavigationPage = new MyNavigationPage (currentPage);
    
        return MainNavigationPage;
    }
    
    // ...
    

    From LoginPage.cs:

    //...
    public async void OnLoginButtonClicked (object sender, EventArgs e)
    {
        //...
        App currentApp = (App)Application.Current;
        App.MainNavigationPage.Clear ();
        Application.Current.MainPage = currentApp.GetMainPage ();
        //...
    }
    

    And feel free to rag on this, but if you do, please offer alternatives lol.

  • LukeConnerLukeConner USMember, University ✭✭

    In this instance they can use the app without being logged in. If they are logged in they have added functionality. I have one other page that will work in the same way, just getting other types of data. They all send the values back to the main page of the app to handle once selected.

  • LukeConnerLukeConner USMember, University ✭✭

    I've been trying to find an example of how that works. The ones I found were only partial and I don't quite understand it yet.

  • LukeConnerLukeConner USMember, University ✭✭

    After looking at the messagingcenter docs on here it seems much simpler than the examples I've looked at. I think that's probably the way to go.

Sign In or Register to comment.