Для локального хранения persistent data
(данные, которые должны оставаться, даже когда пользователь закрывает приложение), вы можете использовать Изолированное хранилище .
Итак, в событии Deactivated
вашего приложения вы можете записать имя страницы в изолированное хранилище следующим образом:
//You get the Isolated Storage for your app (other apps can't access it)
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
//if the file already exists, delete it (since we're going to write a new one)
if (isf.FileExists("lastpage.txt")) isf.DeleteFile("lastpage.txt");
using (var isoFileStream = new IsolatedStorageFileStream("lastpage.txt", FileMode.OpenOrCreate, isf))
{
//open a StreamWriter to write the file
using (var sw = new StreamWriter(isoFileStream))
{
//NavigationService.CurrentSource returns the current page
//we can write this to the file
sw.WriteLine((Application.Current.RootVisual as PhoneApplicationFrame).CurrentSource.ToString());
}
}
Это запишет имя текущей страницы в изолированное хранилище. Затем в методе OnNavigatedto
вашей главной страницы (страница, которая сначала открывается нормально) вы можете прочитать имя файла и перейти к нему:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
string lastpage = string.Empty;
if (isf.FileExists("lastpage.txt"))
{
using (var isoFileStream = new IsolatedStorageFileStream("lastpage.txt", FileMode.Open, isf))
{
//read the file using a StreamReader
using (var sr = new StreamReader(isoFileStream))
{
//get the uri we wrote and then convert it from a String to a Uri
lastpage = sr.ReadLine().Replace("file:///", "");
}
}
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri(lastpage, UriKind.Relative));
}
base.OnNavigatedTo(e);
}
Это должно прочитать файл, который вы сохранили, а затем преобразовать строку в фактический URI, который вы можете передать в NavigationService
.
После этого вы можете удалить текстовый файл после его прочтения, чтобы он не всегда продолжал переходить на эту страницу.