2012-03-11 131 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