如何在运行时改变中的MainPage形式xamarin? [英] How to Change MainPage in xamarin forms at runtime?

查看:907
本文介绍了如何在运行时改变中的MainPage形式xamarin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xamarin形式,RootPage与主从布局。我的任务是显示用户成功登录后页面。我使用的蔚蓝移动服务进行登录。我花更多的时间来获得result.I看到了一些其他的解决方案,但这些解决方案不会使主详细的expected.Finally我得到了解决。

下面是code在app.cs

 公共应用程序()
    {     客户端=新MobileServiceClient(你蔚蓝的网址,你的万能钥匙);
        LoadMainPage();    }公共无效LoadMainPage()
    {
        如果(Client.CurrentUser == NULL)
        {
            的MainPage =新NavigationPage(新SplashPage());
        }
        其他
        {
            =的MainPage新RootView();;
        }
    }

在登录页

 异步无效OnLoginClicked(对象发件人,EventArgs参数)
    {
        MobileServiceUser用户;        尝试
        {
            用户=等待DependencyService.Get<IMobileClient>().LoginAsync(MobileServiceAuthenticationProvider.Facebook);
            Application.Current.MainPage =新RootView();
            等待Navigation.PopToRootAsync();        }
        赶上(InvalidOperationException异常前)
        {
            如果(ex.Message.Contains(认证已被取消))
            {
                //messageLabel.Text =验证用户取消;
            }
        }
        赶上(异常前)
        {
          // messageLabel.Text =验证失败;
        }    }


解决方案

您需要看做导航,不改变这些路径的路线。看看这里的Xamarin导航文档:<一href=\"https://developer.xamarin.com/guides/cross-platform/xamarin-forms/getting-started/introduction-to-xamarin-forms/#Navigation\" rel=\"nofollow\">https://developer.xamarin.com/guides/cross-platform/xamarin-forms/getting-started/introduction-to-xamarin-forms/#Navigation

 等待Navigation.PushModalAsync(新LoginPage());

In xamarin forms,RootPage with master detail Layout. My task is to show that page after user successful login. I am using azure mobile service for login. I spend more time to get result.I saw some other solutions but those solution does not render master detail as expected.Finally i got the solution.

Here is the code in 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();;
        }


    }

In Login page

 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";
        }

    }

解决方案

You need to look at doing navigation, not changing the routes for these paths. Take a look at the Xamarin Navigation docs here: https://developer.xamarin.com/guides/cross-platform/xamarin-forms/getting-started/introduction-to-xamarin-forms/#Navigation

await Navigation.PushModalAsync(new LoginPage());

这篇关于如何在运行时改变中的MainPage形式xamarin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆