2012-03-11 153 views
1

我想創建一個用戶在我的Windows Phone應用程序中打開多個webbrowser實例的功能,但是我一直沒能找到任何文件解釋如何完成這個功能。更確切地說,我想模仿默認的Windows Phone Internet Explorer應用程序提供的標籤式瀏覽體驗。 A已經有一個web瀏覽器isntance正常工作與導航和whatnot,但我怎麼能夠添加多個「標籤」實例,這樣每個webbrowser實例可能在一個單獨的頁面上同時? (我相對較新的C#和Windows手機,所以任何代碼,鏈接,或詳細的解釋將不勝感激。)預先感謝!標籤式網頁瀏覽器控件

我的代碼如下:

MainPage.xaml中

<Grid x:Name="LayoutRoot"> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <my:FullWebBrowser Name="TheBrowser" Grid.RowSpan="2" InitialUri="http://www.google.com" Height="800" Margin="0,0,0,-690" /> 

</Grid> 

TabsPage.xaml

<Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0"></Grid> 
</Grid> 


<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar IsVisible="True"> 
     <shell:ApplicationBarIconButton IconUri="/Icons/appbar.new.rest.png" IsEnabled="True" Text="new" x:Name="NewBtn" Click="NewBtn_Click"/> 
       </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 

眼下TabsPage.xaml是除了應用程序欄圖標應該允許空白用於在點擊時創建新的webbrowser實例。

回答

0

我不會爲選項卡創建單獨的頁面,但我的第一個目的是創建多個瀏覽器控件,並且只顯示當前選項卡的瀏覽器控件。

編輯:

通常沒有人會做,但我寫了一個簡單的多標籤browserlike應用爲您服務。這是非常原始的,但你會明白。順便說一句,你應該更加努力,拿出一個解決方案,這是真的不辛苦......這是MainPage.xaml中:

<Grid x:Name="LayoutRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <TextBox x:Name="UrlTextBox" 
      KeyDown="UrlTextBox_KeyDown" /> 
    <Grid x:Name="BrowserHost" 
      Grid.Row="1" /> 
</Grid> 

<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar> 
     <shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem Text="1" 
              Click="TabMenuItem_Click" /> 
      <shell:ApplicationBarMenuItem Text="2" 
              Click="TabMenuItem_Click" /> 
      <shell:ApplicationBarMenuItem Text="3" 
              Click="TabMenuItem_Click" /> 
      <shell:ApplicationBarMenuItem Text="4" 
              Click="TabMenuItem_Click" /> 
     </shell:ApplicationBar.MenuItems> 
    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 

下面是MainPage.xaml.cs中:

public partial class MainPage : PhoneApplicationPage 
{ 
    private const int NumTabs = 4; 

    private int currentIndex; 
    private string[] urls = new string[NumTabs]; 
    private WebBrowser[] browsers = new WebBrowser[NumTabs]; 

    public MainPage() 
    { 
     InitializeComponent(); 
     ShowTab(0); 
    } 

    private void ShowTab(int index) 
    { 
     this.currentIndex = index; 
     UrlTextBox.Text = this.urls[this.currentIndex] ?? ""; 
     if (this.browsers[this.currentIndex] == null) 
     { 
      WebBrowser browser = new WebBrowser(); 
      this.browsers[this.currentIndex] = browser; 
      BrowserHost.Children.Add(browser); 
     } 
     for (int i = 0; i < NumTabs; i++) 
     { 
      if (this.browsers[i] != null) 
      { 
       this.browsers[i].Visibility = i == this.currentIndex ? Visibility.Visible : Visibility.Collapsed; 
      } 
     } 
    } 

    private void UrlTextBox_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Enter) 
     { 
      Uri url; 
      if (Uri.TryCreate(UrlTextBox.Text, UriKind.Absolute, out url)) 
      { 
       this.urls[this.currentIndex] = UrlTextBox.Text; 
       this.browsers[this.currentIndex].Navigate(url); 
      } 
      else 
       MessageBox.Show("Invalid url"); 
     } 
    } 

    private void TabMenuItem_Click(object sender, EventArgs e) 
    { 
     int index = Int32.Parse(((ApplicationBarMenuItem)sender).Text) - 1; 
     ShowTab(index); 
    } 
} 
+0

謝謝@Kylerrr我相信我明白你的建議,但我不知道如何實施該解決方案。我已經編輯了我原來的帖子,以包含更多信息,也許你可以幫忙?我如何在TabsPage.xaml應用程序欄按鈕單擊事件上創建新的Web瀏覽器控件,以在上一個Web瀏覽器控件上顯示爲新瀏覽實例(並在每個添加的控件之間切換)?你能否包含一些示例代碼,以便我可以玩弄並檢查你的解決方案?再次感謝。 – Matthew 2012-03-12 02:15:44

+0

你的編輯明確地幫助我理解正確的過程,我不確定如何最好地創建瀏覽器的新實例,具體取決於打開的標籤數量,但它似乎解決了一個簡單的URL和Web瀏覽器控件陣列, showtab方法也是我遇到的問題。謝謝,我感謝你用我能理解的術語,希望別人能找到你的解決方案,就像我擁有的​​一樣有益! – Matthew 2012-03-13 02:22:04

+0

我試圖將您的標籤瀏覽示例分成兩個獨立的頁面,一個顯示當前瀏覽索引(MainPage.xaml),然後是另一個頁面來控制當前瀏覽器的創建和選擇,但我無法決定如何通過兩頁之間的數據來完成這個?你認爲作爲分離這兩個頁面之間邏輯的最佳方式是什麼?我已將大部分邏輯放在TabsPage.xaml中,並試圖在MainPage.xaml上顯示選定的瀏覽器索引,這就是我卡住的地方。 – Matthew 2012-03-25 20:08:23

相關問題