簡體   English   中英

通過Facebook登錄后的Xamarin.forms主頁

[英]Xamarin.forms Home Page after login via facebook

我正在使用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>

您需要做的只是設置(而不是在當前頁面頂部推送新頁面)新頁面。 就您而言,在異步方法內的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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM