c# - Error in web browser control navigating -
in web browser app wp7, have 2 xaml pages. 1 mainpage.xaml , other web.xaml. have youtube button in mainpage.xaml, if click on it, navigates youtube.com in web.xaml. if press device button(to navigate mainpage) after youtube navigated, there no error. if press button while youtube navigating throws error. error in recording history think(i have history page record history). error - "cannot write closed textwriter". error occur sometime someother sites too. have added image of error. can me this? in advance help!
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 line--->filestream = historyfile.openfile("history.txt", system.io.filemode.append, fileaccess.write);--->error in 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 line.** }); } historystack_index += 1; } fromhistory = false; navigationcancelled = false; } 
if understand correctly having 2 handlers navigated event (onnavigatedfrom , browsers_navigated).
the problem in onnavigatedfrom calling stream.close(); stream.writeline fail next time called since stream closed.
try moving stream.close(); application close event , use stream.flush() after stream.writeline.
Comments
Post a Comment