通过facebook登录后,Xamarin.forms主页

时间:2017-08-08 07:20:15

标签: c# xaml xamarin xamarin.forms cross-platform

我正在创建一个使用xamarin.forms跨平台的移动应用程序,需要通过facebook登录,我得到了这个工作,但是当用户登录后被定向到主页时,我编码的侧面菜单没有显示。标签栏显示一个向左箭头返回,返回时没有意义。对此表示感谢,谢谢。

Droid项目中的FacebookRender类

[assembly: ExportRenderer(typeof(Login), typeof(loyaltyworx1.Droid.FacebookRender))]
    namespace loyaltyworx1.Droid
    {
        public class FacebookRender : PageRenderer
        {
            public FacebookRender()
            {
                var activity = this.Context as Activity;

        var auth = new OAuth2Authenticator(
            clientId: "",
            scope: "",
            authorizeUrl: new Uri("https://www.facebook.com/dialog/oauth/"),
            redirectUrl: new Uri("https://www.facebook.com/connect/login_success.html")
            );

        auth.Completed += async (sender, eventArgs) =>
        {
            if (eventArgs.IsAuthenticated)
            {
                var accessToken = eventArgs.Account.Properties["access_token"].ToString();
                var expiresIn = Convert.ToDouble(eventArgs.Account.Properties["expires_in"]);
                var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn);

                var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, eventArgs.Account);
                var response = await request.GetResponseAsync();
                var obj = JObject.Parse(response.GetResponseText());

                var id = obj["id"].ToString().Replace("\"", "");
                var name = obj["name"].ToString().Replace("\"", "");

                await App.NavigateToProfile(string.Format("Hello {0}", name));
            }
            else
            {
                await App.NavigateToProfile("Invalid Login");
            }
        };
        activity.StartActivity(auth.GetUI(activity));
    }
}

}

App.cs

public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new MainPage());

    }
    public async static Task NavigateToProfile(string message)
    {
        await App.Current.MainPage.Navigation.PushAsync(new Profile(message));
    }

MainPage.xaml.cs中

 public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private async void LoginBtn_Clicked(object sender, EventArgs e)
    {

        await Navigation.PushAsync(new Login());
    }
}

Login.xaml.cs

public partial class Login : MasterDetailPage
{
    public List<MasterPageItem> menuList { get; set; }

    public Login ()
    {
        InitializeComponent ();
        menuList = new List<MasterPageItem>();
        var page1 = new MasterPageItem() { Title = "Item 1", Icon = "home.png", TargetType = typeof(Page1) };
        var page2 = new MasterPageItem() { Title = "Item 2", Icon = "settings.png", TargetType = typeof(Page2) };
        var page3 = new MasterPageItem() { Title = "Item 3", Icon = "home.png", TargetType = typeof(Page1) };
        var page4 = new MasterPageItem() { Title = "Item 4", Icon = "settings.png", TargetType = typeof(Page2) };

        menuList.Add(page1);
        menuList.Add(page2);

        navigationDrawerList.ItemsSource = menuList;
        Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(Page1)));
    }
    private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
    {

        var item = (MasterPageItem)e.SelectedItem;
        Type page = item.TargetType;

        Detail = new NavigationPage((Page)Activator.CreateInstance(page));
        IsPresented = false;
    }
}

Login.xaml

<?xml version="1.0" encoding="utf-8" ?>

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="loyaltyworx1.Login">

    <MasterDetailPage.Master>
        <ContentPage Title="Menu"
                 BackgroundColor="#e8e8e8">

            <StackLayout Orientation="Vertical">

                <!-- 
             This StackLayout you can use for other
             data that you want to have in your menu drawer
        -->
                <StackLayout BackgroundColor="#e74c3c"
                     HeightRequest="100">

                    <Label Text="Menu"
                 FontSize="20"
                 VerticalOptions="CenterAndExpand"
                 TextColor="White"
                 HorizontalOptions="Center"/>
                </StackLayout>

                <ListView x:Name="navigationDrawerList"
                  RowHeight="60"
                  SeparatorVisibility="None"
                  BackgroundColor="#e8e8e8"
                  ItemSelected="OnMenuItemSelected">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>

                                <!-- Main design for our menu items -->
                                <StackLayout VerticalOptions="FillAndExpand"
                             Orientation="Horizontal"
                             Padding="20,10,0,10"
                             Spacing="20">

                                    <Image Source="{Binding Icon}"
                         WidthRequest="40"
                         HeightRequest="40"
                         VerticalOptions="Center" />

                                    <Label Text="{Binding Title}"
                         FontSize="Medium"
                         VerticalOptions="Center"
                         TextColor="Black"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>

        </ContentPage>
    </MasterDetailPage.Master>

    <MasterDetailPage.Detail>
        <NavigationPage>

        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

1 个答案:

答案 0 :(得分:0)

您需要做的就是设置(不在当前页面顶部推新页面)新页面。在您的情况下,在异步方法内的App类上替换:

public async static Task NavigateToProfile(string message)
{
    await App.Current.MainPage.Navigation.PushAsync(new Profile(message));
}

使用:

public async static Task NavigateToProfile(string message)
{
    await App.Current.MainPage = new Profile(message);
}