开发者

WebBrowser control not working

开发者 https://www.devze.com 2023-02-22 01:25 出处:网络
I\'m developing a Windows Phone 7 app with C#. I\'m trying to do a login with Facebook Login Page. Here is my cs source:

I'm developing a Windows Phone 7 app with C#.

I'm trying to do a login with Facebook Login Page.

Here is my cs source:

namespace FacebookLogin
{
    public partial class MainPage : PhoneApplicationPage
    {
        private const string appId = "xxx";
        private const string apiKey = "xxx"; //insert your apps key here... see developers.facebook.com/setup/
        private readonly string[] extendedPermissions = new[] { "user_about_me" };
        private bool loggedIn = false;
        private FacebookClient fbClient;
        PhoneApplicationService appService = PhoneApplicationService.Current;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            fbClient = new FacebookClient();
            FacebookLoginBrowser.Loaded += new RoutedEventHandler(FacebookLoginBrowser_Loaded);
            //FacebookLoginBrowser.Navigated +=new EventHandler<System.Windows.Navigation.NavigationEventArgs>(FacebookLoginBrowser_Navigated);
        }

        private void FacebookLoginBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            if (!loggedIn)
            {
                LoginToFacebook();
            }
        }

        private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            FacebookOAuthResult oauthResult;
            if (FacebookOAuthResult.TryParse(e.Uri, out oauthResult))
            {
                if (oauthResult.IsSuccess)
                {
                    fbClient = new FacebookClient(oauthResult.AccessToken);
                    loggedIn = true;
                    loginSucceeded();
                }
                else
                {
                    MessageBox.Show(oauthResult.ErrorDescription);
                }
            }
        }

        private void LoginToFacebook()
        {
            TitlePanel.Visibility = Visibility.Visible;
            FacebookLoginBrowser.Visibility = Visibility.Visible;
            InfoPanel.Visibility = Visibility.Collapsed;

            var loginParameters = new Dictionary<string, object>
                                      {
                                          { "response_type", "token" }
                                          // { "display", "touch" } // by default for wp7 builds only (in Facebook.dll), display is set to touch.
                                      };

            var navigateUrl = FacebookOAuthClient.GetLoginUrl(appId, null, extendedPermissions, loginParameters);

            FacebookLoginBrowser.Navigate(navigateUrl);
        }

        // At this point we have an access token so we can get information from facebook
        private void loginSucceeded()
        {
            TitlePanel.Visibility = Visibility.Visible;
            FacebookLoginBrowser.Visibility = Visibility.Collapsed;
            InfoPanel.Vi开发者_如何学Csibility = Visibility.Visible;

            fbClient.GetCompleted +=
                (o, e) =>
                {
                    if (e.Error == null)
                    {
                        var result = (IDictionary<string, object>)e.GetResultData();
                        Dispatcher.BeginInvoke(() => MyData.ItemsSource = result);
                    }
                    else
                    {
                        MessageBox.Show(e.Error.Message);
                    }
                };
            fbClient.GetAsync("/me");
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs args)
        {
            appService.State["fbClient"] = fbClient;
            base.OnNavigatedFrom(args);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs args)
        {
            base.OnNavigatedTo(args);
        }
    }
}

And here is my XAML code:

<phone:PhoneApplicationPage 
    x:Class="FacebookLogin.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28" Visibility="Collapsed">
            <TextBlock x:Name="ApplicationTitle" Text="MI APLICACIÓN" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="nombre de la página" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:WebBrowser HorizontalAlignment="Left" Name="FacebookLoginBrowser" Height="607" VerticalAlignment="Top" Width="450" />
            <Grid x:Name="InfoPanel" Grid.Row="1">
                <ListBox x:Name="MyData" Height="657" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid HorizontalAlignment="Stretch" MinWidth="500">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding Key}"/>
                                <TextBlock Grid.Column="1" Text="{Binding Value}" TextWrapping="Wrap"/>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </Grid>
    </Grid>
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Botón 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Botón 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="Elemento de menú 1"/>
                <shell:ApplicationBarMenuItem Text="Elemento de menú 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>

Where is the error? FacebookLoginBrowser_Navigated isn't never reached.


Here's the answer (and I knew it before even looking at your code, but I did doublecheck). The WP7 Browser control defaults to having JavaScript disabled (why they did that is beyond me). SO, you need to set the "IsScriptEnabled" property of the web browser control to True (you can do this in code or in XAML), and then I bet everything will work.


You have your FacebookLoginBrowser_Navigated event handler commented out. That is the correct one to use. Not a RoutedEventHandler just an EventHandler.

Also it might not be a bad idea just to set the eventhandler in design view on the browser control if it's not absolutely necessary to dynamically create the event.

0

精彩评论

暂无评论...
验证码 换一张
取 消