简体   繁体   中英

wp8 C# browser freezing

I'm back to implement a code, but something is going wrong. Simply by browsing the web, the browser freezes, locking the cell and preventing its closure. What is he missing? Help me please! The code I use is exactly this:

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

    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);
 }
}

You only have one for loop to determine if this is causing the problem then comment it out and loop and run the app. If it is causing the problem then either put it in a try catch of after 10 loop break the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM