单击“返回”退出应用程序WP7 上的按钮
我知道在 WP7 中不可能以编程方式退出应用程序。那么我该如何处理以下需求呢? 我的主页是空的,唯一的目的是进行测试: 如果用户从未填写首选项页面,则重定向到 Page_B.xaml(收集用户首选项的页面,例如运行应用程序所需的语言和其他信息)。否则重定向到 Page_A.xaml。 因此,用户显示的第一页是 Page_A 或 Page_B(取决于这是否是他/她第一次运行应用程序)。
问题是这样的: 当用户在 Page_A 或 Page_B 中选择硬件“后退”按钮时,我想退出应用程序。相反,他被重定向到主页,但什么也没有显示。 因此,当用户在 Page_A 或 Page_B (OnBackKeyPress()) 中选择“后退”时,或者更一般地,当用户使用“后退”按钮进入 MainPage.xaml 时,我需要退出应用程序。 有没有办法退出应用程序而不显示空的 MainPage.xaml? 谢谢你的建议。 Emilio
这里是 MainPage.xaml 中的简化代码:
public MainPage(){
InitializeComponent();
if (phoneAppService.State.TryGetValue("currentLanguage", out someObject))
{ // Yes: go on
var uri = "/Pages/Page_A.xaml";
this.Dispatcher.BeginInvoke(() => this.NavigationService.Navigate(new Uri(uri, UriKind.Relative)));
}
else
{ // No: select language before proceeding
var uri = "/Pages/Page_B.xaml";
this.Dispatcher.BeginInvoke( () => this.NavigationService.Navigate(new Uri(uri, UriKind.Relative)));
}
}
**// if previous page was Page_A or Page_B then exit application**
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string sourcePage = "";
if (NavigationContext.QueryString.TryGetValue("from", out sourcePage)) {
if ((string.Compare(sourcePage.ToString(), "Page_A")) == 0 ? true : false) {
**// EXIT APPLICATION**
}
if ((string.Compare(sourcePage.ToString(), "Page_B")) == 0 ? true : false) {
**// EXIT APPLICATION**
}
}
base.OnNavigatedTo(e);
}
Page_A.xaml 有以下代码将信息发送到 MainPage。
// Back Button pressed: notify MainPage so it can exit application
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
base.OnBackKeyPress(e);
}
Page_B.xaml 具有以下代码将信息发送到 MainPage。
// Back Button pressed: notify MainPage so it can exit application
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var uri = "/MainPage.xaml?from=Page_B";
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
base.OnBackKeyPress(e);
}
I know that in WP7 it is not possible to exit application programatically. So haw can I handle the following need?
My MainPage is empty, and has has the only purpose to make a test:
if user never filled a preference page, redirects to Page_B.xaml (a page which collects his preferences, such as language aond other info which are needed in order to run the app). Otherwise redirect to Page_A.xaml.
So the first page that user is shown is either Page_A or Page_B (depending if this is the first time he/she runs the app).
HERE IS THE PROBLEM:
when user select the hardware "Back" button while in Page_A or Page_B, I want to quit application. Instead he is redirected to the mainPage, which shows nothing.
So I need to exit applicatin when user selects "Back" in Page_A or Page_B (OnBackKeyPress()) , or more generally when user comes to MainPage.xaml using the Back button.
Is there a way to exit application without showing the empty MainPage.xaml?
Thanks for your advice.
Emilio
here is the simplified code in MainPage.xaml:
public MainPage(){
InitializeComponent();
if (phoneAppService.State.TryGetValue("currentLanguage", out someObject))
{ // Yes: go on
var uri = "/Pages/Page_A.xaml";
this.Dispatcher.BeginInvoke(() => this.NavigationService.Navigate(new Uri(uri, UriKind.Relative)));
}
else
{ // No: select language before proceeding
var uri = "/Pages/Page_B.xaml";
this.Dispatcher.BeginInvoke( () => this.NavigationService.Navigate(new Uri(uri, UriKind.Relative)));
}
}
**// if previous page was Page_A or Page_B then exit application**
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string sourcePage = "";
if (NavigationContext.QueryString.TryGetValue("from", out sourcePage)) {
if ((string.Compare(sourcePage.ToString(), "Page_A")) == 0 ? true : false) {
**// EXIT APPLICATION**
}
if ((string.Compare(sourcePage.ToString(), "Page_B")) == 0 ? true : false) {
**// EXIT APPLICATION**
}
}
base.OnNavigatedTo(e);
}
Page_A.xaml has the following code to send information to MainPage.
// Back Button pressed: notify MainPage so it can exit application
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
base.OnBackKeyPress(e);
}
Page_B.xaml has the following code to send information to MainPage.
// Back Button pressed: notify MainPage so it can exit application
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var uri = "/MainPage.xaml?from=Page_B";
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
base.OnBackKeyPress(e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一种相当常见的场景,要么在第一次运行应用程序时执行一次性任务,要么需要登录才能使用该应用程序。我建议不要将其写为一个完整的页面,而是将 UserControl 放在主页上的全屏弹出窗口中。这样,按一次返回键将始终退出您的应用程序。
This is a fairly common scenario with either doing a one-off task on 1st time run of an app, or if you need to login to use the app at all. Rather than writing this as a full page I'd recommend putting a UserControl in a full-screen Popup over your main page. That way a single Back key press will always exit your app.
您可能想要重组您的应用程序。根本没有 MainPage,始终加载 PageA。如果用户尚未设置首选项,只需将其重定向到 PageB,他们将设置首选项并点击“后退”按钮,将其带回到 PageA。由于应用程序现在已完成所需的设置,因此可以正常显示 PageA。
如果您确实必须使用 3 页方案,则可以获取 非线性导航服务发挥其魔力。
You may want to restructure your application. Do not have a MainPage at all, always load PageA. If the user hasn't set preferences, just redirect them to PageB, they'll set preferences and hit the Back button which takes them back to PageA. Since the app now has the settings it needs it can display PageA normally.
If you really must use the 3 page scheme, you may be able to get the NonLinear Navigation Service to work its magic.
您可以通过在 App 类上使用静态布尔变量(例如 ForceExitApplication)并在 Page_A 或 Page_B 上将其设置为 true 来实现此目标。在 MainPage 上,您将检查此变量,如果其设置为 true,则退出应用程序:
工作)
市场提交失败)
但是,我会采取不同的方式实施这种行为。看来您想要实现的目标 - 在主页以外的位置退出应用程序 - 违反了 WP7 准则,如果是这样,您的应用程序在提交时可能会被拒绝,直到您解决此问题。
You could achieve this goal by e.g. having a static boolean variable on the App class, e.g. ForceExitApplication and set this to true on Page_A or Page_B. On MainPage you would check this variable and if its set to true you exit the application:
work)
fail marketplace submission)
I would, however be vary of implementing this behaviour. It seems that what you are trying to achieve - exiting the application at a point other than the main page - is against the WP7 guidelines and if this is so, your application is likely to get rejected when submitting until you fix this issue.
在 Mango 中,您可以使用以下方法:
在 app.cs 中将事件处理程序添加到 RootFrame.Naviged 事件:
然后在事件处理程序中,我们可以利用导航后台堆栈:
这段代码的作用是:
首先,我们检查是否已导航到页面 A 或 B。然后检查导航堆栈中是否有任何页面。 (它应该至少是一个,因为我们已经从主页重定向了)。然后,我们从后台日志中删除最后一个条目。现在,用户按下“后退”按钮,应用程序将退出。
您可以在此处阅读有关 BackStack 的更多信息: http:// msdn.microsoft.com/en-us/library/hh394012(v=vs.92).aspx
In Mango you could use the following approach:
in app.cs add event handler to RootFrame.Navigated event:
Then within the event handler, we could utilize the navigation backstack:
What does this code do:
First, we are checking, if we have navigated to page A or B. Then we are checking, if we have any pages in navigation backstack. (It should be at least one, because, we had already been redirected from mainPage). Then, we are removing the last entry from the backstack journal. Now, of a user will press the "Back" button, the application will exit.
You could read more about BackStack here: http://msdn.microsoft.com/en-us/library/hh394012(v=vs.92).aspx
大多数页面的可视化树中的根项是网格。将两个 UI 组织成由 stackpanel 托管的两个网格并将每个网格的可见性绑定到页面依赖属性以便该属性控制哪个可见是很简单的。一个页面没有理由不能同时满足这两个目的。
The root item in the visual tree of most pages is a grid. It is trivial to organise your two UIs into two grids hosted by a stackpanel and bind the visibility of each to a page dependency property so that this property controls which is visible. There is no reason a single page can't serve both purposes.