如何在运行时更改xamarin表单中的MainPage?

Nar*_*dra 1 azure azure-mobile-services asp.net-web-api2 xamarin-forms

在xamarin表单中,RootPage具有主细节布局.我的任务是在用户成功登录后显示该页面.我正在使用azure移动服务登录.我花了更多的时间来获得结果.我看到了其他一些解决方案,但这些解决方案并没有像预期的那样呈现主要细节.最后,我得到了解决方案.

这是app.cs中的代码

public App()
    {

     Client = new MobileServiceClient("your azure url", "your master key");
        LoadMainPage();

    } public void LoadMainPage()
    {
        if (Client.CurrentUser == null)
        {
            MainPage=new NavigationPage(new SplashPage());
        }
        else
        {
            MainPage = new RootView();;
        }


    }
Run Code Online (Sandbox Code Playgroud)

在登录页面

 async void  OnLoginClicked(object sender, EventArgs args)
    {
        MobileServiceUser user;

        try
        {
            user = await DependencyService.Get<IMobileClient>().LoginAsync(MobileServiceAuthenticationProvider.Facebook);
            Application.Current.MainPage=new RootView();
            await Navigation.PopToRootAsync();

        }
        catch (InvalidOperationException ex)
        {
            if (ex.Message.Contains("Authentication was cancelled"))
            {
                //messageLabel.Text = "Authentication cancelled by the user";
            }
        }
        catch (Exception ex)
        {
          //  messageLabel.Text = "Authentication failed";
        }

    }
Run Code Online (Sandbox Code Playgroud)

Chr*_*SFT 5

您需要查看导航,而不是更改这些路径的路径.在这里查看Xamarin导航文档:https://developer.xamarin.com/guides/cross-platform/xamarin-forms/getting-started/introduction-to-xamarin-forms/#Navigation

await Navigation.PushModalAsync(new LoginPage());
Run Code Online (Sandbox Code Playgroud)