对用户所在的最后访问过的页面进行逻辑删除

时间:2012-01-03 00:54:20

标签: silverlight windows-phone-7.1 windows-phone-7

我想在用户所在的最后一页上标记逻辑删除,并在用户返回应用程序时检索它。互联网上的所有墓碑化示例都涉及保存一些数据或用户以某种方式编辑的页面状态(即文本框中的输入文本)。在我的应用程序中,我没有任何东西供用户修改/编辑,所以我只想保存用户所在的最后访问过的页面。我尝试使用一些使用PhoneApplicationService.Current.State的在线示例,但没有成功。

感谢所有想帮助我解决此问题的人!

3 个答案:

答案 0 :(得分:3)

要本地存储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());
       }
}

这会将当前页面的名称写入Isolated Storage。然后,在主页的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);
}

这应该在你保存的文件中读取,然后将字符串转换为可以传递给NavigationService的实际URI。

然后,您可以在读取文本文件后将其删除,这样它就不会一直跳到该页面。

答案 1 :(得分:2)

此外,您可以使用它来获取页面名称string PageName = (Application.Current.RootVisual as PhoneApplicationPage).Name;以获取当前页面名称

答案 2 :(得分:1)

虽然我同意所有上述选项都是可能的,但它们并不是在WP7中做某事的正确方法。

最好在应用程序的开头构建导航页面以正确控制导航,它还有助于在使用应用程序时管理后退键事件并防止出现打嗝。

请参阅此处了解实现此目的的一个示例: Properly Exiting Silverlight-based WP7

完成此操作后,请使用有关在隔离存储/应用程序设置中存储值的建议,然后只存储“当前页面”状态值(例如Page.GamePage的值),然后应用程序导航将相应地引导您。 但是当你只需要存储当前页面本身时要小心,因为你还需要保存任何值的正确状态或用户在墓碑上输入数据时,上述建议应该引导你朝着正确的方向前进。

希望这有帮助

相关问题