在新选项卡事件中打开,以便为wp7进行Web浏览器控制

时间:2012-05-24 16:46:02

标签: c# windows-phone-7

如何使用上下文菜单(对于像IE这样的链接)使用在新标签页中打开事件,以便为使用Windows Phone 7的网络浏览器控件创建以下标签?

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 个答案:

没有答案