1

我正在用C#開發Windows Phone 7應用程序。WebBrowser控件不起作用

我正在嘗試使用Facebook登錄頁面進行登錄。

這裏是我的CS來源:

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.Visibility = 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); 
     } 
    } 
} 

這是我的XAML代碼:

<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> 

哪裏錯誤? FacebookLoginBrowser_Navigated永遠不會到達。

回答

4

下面是答案(我甚至在查看代碼之前就知道它,但我做了雙重檢查)。 WP7瀏覽器控件默認爲禁用JavaScript(爲什麼他們這樣做超出了我)。因此,您需要將Web瀏覽器控件的「IsScriptEnabled」屬性設置爲True(您可以在代碼或XAML中執行此操作),然後我敢打賭一切都會起作用。

+0

就這麼簡單。一天半尋找答案。非常感謝你。 – VansFannel 2011-04-06 13:08:30

+0

@OffBySome,我認爲這是有禮貌的添加一個評論,提及你編輯的內​​容......我能看到的是你刪除了87個字符...... – DevTheo 2012-01-19 17:05:14

0

您已將您的FacebookLoginBrowser_Navigated事件處理程序註釋掉。這是正確的使用。不是RoutedEventHandler只是一個EventHandler。

另外,如果不是絕對需要動態創建事件,那麼只需在瀏覽器控件的設計視圖中設置事件處理程序也不失爲一個好主意。

+0

是的,我已經這樣做了,但我得到同樣的問題。當我點擊登錄按鈕時,它什麼都不做(並且未到達FacebookLoginBrowser_Navigated)。 – VansFannel 2011-04-06 08:57:04