简体   繁体   English

Windows Phone 8.1运行时中的页面和应用程序事件行为

[英]Page and App events behavior in Windows Phone 8.1 Runtime

I'm having trouble managing the life cycle of my app because I'm unsure exactly which events and functions pertaining to the page/app lifecycle run when. 我无法管理应用程序的生命周期,因为我不确定与页面/应用程序生命周期相关的事件和功能何时运行。 It'd be nice if Microsoft tabulated it all somewhere. 如果Microsoft在某个地方将它们全部制表,那将是很好的。 To be specific, here's what I think I know and what I don't know about the behavior of the each of the following events/functions: 具体来说,以下是我认为和不知道的以下每个事件/功能的行为:

Page.Page() (constructor): Runs when the page is created for the first time. Page.Page() (构造函数):首次创建页面时运行。 Unsure if it runs only once while the app is running, or each time I navigate to the page within a single session. 不确定它是在应用程序运行时仅运行一次,还是每次在单个会话中导航至该页面时都运行。

Page.OnNavigatedTo() : Runs when the page is created and each time it is navigated to. Page.OnNavigatedTo() :创建页面时以及每次导航到页面时运行。

Page.OnNavigatedFrom() : Runs when the user leaves the page. Page.OnNavigatedFrom() :在用户离开页面时运行。 Does not run when the user presses the Start button or the lock screen button while the page is displaying. 当用户在显示页面时按“开始”按钮或“锁定”屏幕按钮时不运行。

App.App() (constructor): Runs only once when the app is launched. App.App() (构造函数):启动应用程序时仅运行一次。

App.Resuming : Fires when the app is brought into view after being minimized/suspended, or when the user unlocks the screen and the app comes into view. App.Resuming :在最小化/挂起应用程序后将其显示在视图中,或者当用户解锁屏幕并显示在应用程序中时触发。 Does not run when the app is started for the first time. 首次启动该应用程序时不运行。

App.Suspending : Fires when the user presses the back button on the home page (ie the app is no longer in view) and when the start button is pressed. App.Suspending :当用户按下主页上的“后退”按钮(即不再显示该应用程序)以及按下“开始”按钮时触发。

Anything I missed or got wrong? 我有什么想念的地方吗? The reason I'm trying to be sure is that my app has a page which runs background audio player which in turn needs a carefully managed system of event subscription/unsibscription, and it often behaves awkwardly if I hit the start button while on that page. 我要确保的原因是我的应用程序有一个页面,该页面运行背景音频播放器,而该页面又需要精心管理的事件订阅/取消订阅系统,并且如果我在该页面上按下“开始”按钮,它的行为通常会很尴尬。 And another issue is that I have a static object in my App.xaml.cs which I'm using across the app and which exposes some events. 另一个问题是我在App.xaml.cs中有一个静态对象,该对象正在整个应用程序中使用,并暴露了一些事件。 Right now I'm subscribing/unsubscribing to the events on each page like so: 现在,我正在订阅/取消订阅每个页面上的事件,如下所示:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    App.globalObject.Event1 += Event1_handler;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    App.globalObject.Event1 -= Event1_handler;
}

But I'm unsure if this is the correct approach (obviously I'm trying to ensure that I'm only subscribed to an event once to avoid the event handler running multiple times). 但是我不确定这是否是正确的方法(显然,我试图确保只预订一次事件,以避免事件处理程序多次运行)。 Should I just subscribe to the event once in the page's constructor instead? 我应该只在页面的构造函数中订阅一次该事件吗?

Edit: I'm using a project template with the SuspensionManager and NavigationHelper classes included, in case that changes anything. 编辑:我正在使用包含SuspensionManagerNavigationHelper类的项目模板,以防万一。

Your code and logic looks OK to me, but as igrali suggested in comments, you can always "clear" the handler before subscribing. 您的代码和逻辑对我来说似乎还可以,但是正如igrali在注释中所建议的那样,您始终可以在订阅前“清除”处理程序。 It's kinda hacky, but it will do the job 这有点hacky,但可以完成工作

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    App.globalObject.Event1 -= Event1_handler;
    App.globalObject.Event1 += Event1_handler;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows Phone 8.1运行时应用程序的xaml页面本身中添加多个AppBar / CommandBar? - How to add Multiple AppBar/CommandBar's in xaml page itself in windows phone 8.1 runtime app? 将数据返回到上一页Windows Phone 8.1 Windows运行时 - Returning data to a previous page Windows Phone 8.1 Windows RunTime Windows Phone 8.1运行时应用 - Windows phone 8.1 runtime apps 如何在Windows Phone 8.1运行时应用程序中显示计划的对话框? - How to show a scheduled dialog in a Windows Phone 8.1 Runtime app? Windows Phone 8.1运行时应用程序项目中缺少命名空间 - Namespace missing in Windows Phone 8.1 runtime app project Windows Phone 8.1运行时应用程序无法获取TextBlock ActualWidth值 - cannot get TextBlock ActualWidth value in windows phone 8.1 runtime app 在Windows Phone 8.1通用应用程序中设置起始页 - Set start page in Windows Phone 8.1 universal app 使用caliburn在Windows Phone 8.1应用中加载页面时的进度栏 - Progressbar when page is loading in windows phone 8.1 app with caliburn 如何在其他页面中使用App ResourceDictionary和MergedDictionaries-Windows Phone 8.1 - How to use App ResourceDictionary and MergedDictionaries in other page - Windows Phone 8.1 更改Windows Phone 8.1应用程序的默认启动页面 - Change default startup page for windows phone 8.1 app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM