Web浏览器控件导航时出错

时间:2012-06-07 13:06:22

标签: c# windows-phone-7

在我的wp7网页浏览器应用中,我有两个xaml页面。一个是mainpage.xaml,另一个是web.xaml。 我在mainpage.xaml中有一个youtube按钮,如果我点击它,它会导航到web.xaml中的youtube.com。但是,如果我在完全导航youtube后按下设备后退按钮(导航到主页),则没有错误。但是,如果我在youtube导航时按下后退按钮,则会抛出错误。我认为记录历史记录时出错(我也有记录历史记录的历史页面)。错误是 - “无法写入已关闭的TextWriter”。对于其他一些站点,此错误也会在某个时间发生。我还添加了该错误的图像。谁能帮我这个?在此先感谢您的帮助!

public partial class Web : PhoneApplicationPage
{
    List<Uri> HistoryStack;
    int HistoryStack_Index;
    bool fromHistory;
    bool navigationcancelled = false;
    public IsolatedStorageFile historyFile = null;
    public IsolatedStorageFileStream filestream = null;
    public StreamWriter stream = null;

    public Web()
    {
        InitializeComponent();
        HistoryStack = new List<Uri>();
        historyFile = IsolatedStorageFile.GetUserStoreForApplication();
        if (historyFile.FileExists("History.txt"))
        {
            Error in this line--->filestream = historyFile.OpenFile("History.txt", System.IO.FileMode.Append, FileAccess.Write);--->Error in this line
            stream = new StreamWriter(filestream);
        }
        else
        {
            filestream = historyFile.OpenFile("History.txt", System.IO.FileMode.Create);
            stream = new StreamWriter(filestream);
        }

        HistoryStack_Index = 0;
        fromHistory = false;
        browsers[this.currentIndex].Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(browsers_Navigated);
        browsers[this.currentIndex].Navigating += new EventHandler<NavigatingEventArgs>(browsers_Navigating);
    }

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        {
            stream.Close();
        }
    }

private void browsers_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    { 
        if (!fromHistory)
        {
            if (HistoryStack_Index < HistoryStack.Count)
            {
                HistoryStack.RemoveRange(HistoryStack_Index, HistoryStack.Count - HistoryStack_Index);
            }
            HistoryStack.Add(e.Uri);
                 //HistoryURL temp = new HistoryURL();
                 //temp.URL = e.Uri.ToString();
                 //app.historyList.Add(temp);
                Thread.Sleep(100);
                Dispatcher.BeginInvoke(() =>
                {
                    string title = (string)browsers[this.currentIndex].InvokeScript("eval", "document.title.toString()");
                    stream.WriteLine(title + ";" + e.Uri.ToString());---> **Error in this line.**
                });
            }

            HistoryStack_Index += 1;
        }
        fromHistory = false;
        navigationcancelled = false;
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,您有两个处理程序用于导航事件(OnNavigatedFrombrowsers_Navigated)。

问题可能是在OnNavigatedFrom中您正在调用stream.Close();,因此,自从流关闭以来,stream.WriteLine将在下次调用时失败。

尝试将stream.Close();移至应用关闭事件,并在stream.Flush()之后使用stream.WriteLine