我为 wp7 创建了一个网络浏览器应用程序。我在应用程序栏菜单项中添加了一些 6 个选项卡。但是现在我想要分别在用户控制页面中的这 6 个选项卡,如果我单击选项卡 2 或任何选项卡,它应该像现在一样工作。
主页.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);
}
}
就像下面的浏览器一样。
谁能帮我这个?谢谢你的帮助!