persistent data
(사용자가 앱을 종료해도 남아 있어야하는 데이터)을 로컬에 저장하려면 Isolated Storage을 사용할 수 있습니다.
그래서, 앱의 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
에 전달할 수 있습니다.
그런 다음 텍스트 파일을 읽은 후에 삭제하면 항상 해당 페이지로 이동하지 않을 수 있습니다.
사용자가 삭제 표시에서 돌아 오면 이미 마지막 페이지로 이동했습니다. 저장할 사용자 지정 데이터가 없으면 아무 것도하지 않고도 작업 할 수 있습니다. – keyboardP
답해 주셔서 감사합니다. 예, 뒤로 버튼으로 작동하지만 사용자가 애플리케이션 아이콘을 눌러 페이지로 다시 돌아올 수 있다면 좋을 것이라고 생각했습니다. 이제 시작 화면이 시작되고 첫 번째 페이지로 이동합니다. 그 일을하는 어떤 방법이 있습니까? –
기술적으로 말하면 사용자가 앱을 다시 시작했음을 의미합니다 (삭제 표시에서 돌아 오는 대신). 이 경우,'PhoneApplicationService.Current '때문에 IsolatedStorage에 써야합니다.State'는 앱이 실행 중이거나 삭제 표시가있는 동안에 만 작동합니다. 앱을 다시 시작하면 '상태'가 지워 지므로 액세스 할 수 없습니다. 잠시 후에 코드를 작성하겠습니다. – keyboardP