我开发了一个记事本,一个主页面,一个编辑页,现在情况是我在主页面按添加命令导航到编辑页面上 ,在编辑页面上按保存然后跳转到主页面……接下来问题来了:我按下手机返回键却返回到了编辑页面上了,这个时候页面是停留在主页面的,应该退出,但为什么又返回到编辑页面上去了?求大神解决。
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = file.OpenFile(_tbx_title.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(_tbx_body.Text);
writer.Close();
}
}
this.NavigationService.Navigate(new Uri("/Mainpage.xaml", UriKind.Relative));
}
这是编辑页面的
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
this.NavigationService.Navigate(new Uri("/edit.xaml", UriKind.Relative));
}这是主页面的添加命令。
PS,因为不知道哪里有问题,所以先贴一点儿代码 求大神看下,小弟谢过了
------解决方案--------------------
在主页面的构造函数中写下:
this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);
下面是方法:
void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
int count = NavigationService.BackStack.Count();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
NavigationService.RemoveBackEntry();
}
}
}
问题解决的话 记得结贴给分哦

------解决方案--------------------
我觉得,如果编辑页面编辑之后一定是回到前一页的话,更好的方法是在保存之后不是通过navigate跳转而是直接用goback回到前一页,就是,不用:
this.NavigationService.Navigate(new Uri("/Mainpage.xaml", UriKind.Relative));
而用
this.NavigationService.GoBack()
------解决方案--------------------
说得很正确。