如何在我的 WP7 应用程序中获取当前页面 [英] How to get current page in my WP7 app

查看:17
本文介绍了如何在我的 WP7 应用程序中获取当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在我的 Windows Phone 7 应用程序中获取当前页面的活动实例?我一直在阅读文档并试图弄清楚无济于事.

Is there a way to get the active instance of the current page in my Windows Phone 7 app? I have been reading documentation and trying to figure it out to no avail.

在我的示例中,我正在运行一个全局循环,该循环调用活动页面的公共方法.

In my example I am running a global loop which calls a public method of the active page.

例如,应用程序加载到 Page1.xaml,如果我的循环在此页面处于活动状态时触发,我需要触发在 Page1 代码隐藏中找到的循环.如果 Page2 处于活动状态,我将需要在 Page2 代码隐藏文件中触发不同的方法.

For example, the application loads to Page1.xaml, if my loop fires with this page active I need to fire a loop found in the Page1 code-behind. If Page2 is active I will need to be firing a different method in the Page2 code-behind file.

注意:我在各个页面中的方法不能是静态的,因为我需要访问实例变量.所以我不仅需要活动页面(可以通过某种变量实现),而且我需要当前页面的实例.

NOTE: My methods in the individual pages cannot be static because I require access to instance variables. So not only do I need the active page (which could be achieved through a variable of some sort), but I need the instance of the current page.

推荐答案

可以从根框架获取当前页面.在项目中生成的默认 App 类具有一个 RootFrame 属性,该属性已经是 PhoneApplicationFrame 类型.Content 属性的值是当前页面,可以转换为 PhoneApplicationPage.

You can get the current page from the root frame. The default App class that is generated in the project has a RootFrame property that is already of type PhoneApplicationFrame. The value of the Content property is the current page and can be cast to PhoneApplicationPage.

var currentPage = ((App)Application.Current).RootFrame.Content as PhoneApplicationPage;

Application 类上的 RootVisual 属性引用了相同的框架,但它是一个 UIElement,因此您可以使用以下内容:

The RootVisual property on the Application class references the same frame, but is a UIElement, so you could use the following instead:

var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;

这篇关于如何在我的 WP7 应用程序中获取当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆