在Window Phone 8.1中OnNavigatedFrom(OnNavigatedFrom in Window Phone 8.1)

编程入门 行业动态 更新时间:2024-10-27 19:18:10
在Window Phone 8.1中OnNavigatedFrom(OnNavigatedFrom in Window Phone 8.1)

我正在从Windows Phone 8迁移到Windows Phone 8.1。

我创建了一个Windows Phone 8.1商店应用程序,Hub应用程序。

该应用程序创建了OnNavigatedTo和OnNavigatedFrom方法

protected override void OnNavigatedTo( NavigationEventArgs e ) { this.navigationHelper.OnNavigatedTo( e ); } protected override void OnNavigatedFrom( NavigationEventArgs e ) { this.navigationHelper.OnNavigatedFrom( e ); }

我在OnNavigatedFrom中放置一个断点并试图关闭应用程序,或者离开应用程序并且没有命中断点,即应用程序没有到达OnNavigatedFrom。

Windows Phone 8应用程序正在打破OnNavigatedFrom。 WP 8.1的机制是否不同? 如果是这样的话?

谢谢。

I am moving from Windows Phone 8 to Windows Phone 8.1.

I Created a Windows Phone 8.1 Store Application, Hub App.

The application created the OnNavigatedTo and OnNavigatedFrom methods

protected override void OnNavigatedTo( NavigationEventArgs e ) { this.navigationHelper.OnNavigatedTo( e ); } protected override void OnNavigatedFrom( NavigationEventArgs e ) { this.navigationHelper.OnNavigatedFrom( e ); }

I put a breakpoint in the OnNavigatedFrom and tried to either close the application, or to leave the application and the breakpoint is not hit, i.e. the application doesn't reach the OnNavigatedFrom.

A Windows Phone 8 application is breaking on the OnNavigatedFrom. Is the mechanism is different with WP 8.1? if so how?

Thanks.

最满意答案

问题似乎发生,因为您正在调试模式(VS附加)中运行。 在这种情况下,您的程序在导航/暂停事件的情况下行为略有不同,要正确测试它,您必须手动调用Suspending事件( Lifecyce事件下拉列表 )。 在正常情况下,两个事件( OnNavigatedFromSuspending )将在您离开应用程序后立即调用。

为了测试它,让我们在OnNavigatedFrom添加一些内容 (基于来自Windows应用商店模板的Hub App ):

protected async override void OnNavigatedFrom(NavigationEventArgs e) { Debug.WriteLine("OnNavigatedFrom"); Hub.Background = new SolidColorBrush(Colors.Red); this.navigationHelper.OnNavigatedFrom(e); }

在这种情况下,当您运行不附带Visual Studio的应用程序时,当您返回应用程序时,背景应为红色 - 这意味着该事件已被触发。

实际上,当转移到WP8.1 WinRT时,还有一个巨大的(IMO)差异 - 当你从暂停状态回来时, OnNavigatedTo 不会被触发 :

注意在Windows Phone上,应用程序挂起时会调用OnNavigatedFrom()。 应用程序恢复时不会调用OnNavigatedTo()。

它只在你导航时被调用。

更多参考: 在页面之间导航 , 生命周期 , 启动,恢复和多任务处理以及应用程序暂停和恢复指南 。

The problem seems to occur, because you are running in Debug mode (VS attached). In this situation your program behaves little different in case Navigation/Suspend events, to test it properly you will have to invoke the Suspending event manually (Lifecyce events dropdown). In normal situation both events (OnNavigatedFrom and Suspending) will be called just after you leave the app.

To test it let's put something in OnNavigatedFrom (basing on Hub App from Windows Store templates):

protected async override void OnNavigatedFrom(NavigationEventArgs e) { Debug.WriteLine("OnNavigatedFrom"); Hub.Background = new SolidColorBrush(Colors.Red); this.navigationHelper.OnNavigatedFrom(e); }

in this case, when you run the app without Visual Studio attached, when you return to the app the background should be red - which means that the event has been fired.

There is, in fact, one more huge (IMO) difference when moving to WP8.1 WinRT - OnNavigatedTo won't be fired when you come back from suspension:

Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.

it's called only when you navigate.

Some more references: Navigating between pages, Lifecycle, Launching, resuming, and multitasking and Guidelines for app suspend and resume.

更多推荐

本文发布于:2023-08-04 15:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Phone   Window   OnNavigatedFrom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!