【问题标题】:Error in web browser control navigatingWeb 浏览器控件导航中的错误
【发布时间】:2012-06-09 01:50:23
【问题描述】:

在我的 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;
    }

【问题讨论】:

  • 流在哪里打开/关闭?
  • @RowlandShaw 现在我编辑了我的帖子。抱歉,我之前忘记添加了!

标签: c# windows-phone-7


【解决方案1】:

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

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

尝试将stream.Close(); 移动到应用程序关闭事件并在stream.WriteLine 之后使用stream.Flush()

【讨论】:

  • 你的意思是我必须添加 stream.close();在 app.xaml.cs(in private void Application_Closing(object sender, ClosingEventArgs e) {}?
  • @mac:是的,因为您在所有导航事件上都使用相同的流,并且您不希望在应用程序结束之前关闭它
  • @Mac:会抛出同样的异常吗?
  • 是的,还是一样!
  • @Mac:将public StreamWriter stream = null; 替换为public static StreamWriter stream。使流静态应保持打开状态,以防页面被释放。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
相关资源
最近更新 更多