[英]windows 8 App accessing page method from App.xaml.cs
Probably a stupid question so I apologise in advance. 可能是一个愚蠢的问题所以我提前道歉。 I am new to building a Windows 8 Store App. 我是构建Windows 8商店应用程序的新手。
I need to run some methods on my page script when the App gets suspended. 当应用程序被挂起时,我需要在我的页面脚本上运行一些方法。 I only have a single page and I have the some public methods in the Page1.xaml.cs file. 我只有一个页面,我在Page1.xaml.cs文件中有一些公共方法。 I want to call them from the OnSuspending() method in the App.xaml.cs file. 我想从App.xaml.cs文件中的OnSuspending()方法调用它们。 I need to make sure that some of the text files are saved. 我需要确保保存一些文本文件。
How can I create a reference to my Page1 script? 如何创建对Page1脚本的引用?
You can try accessing Page1
object from Content
property of current Frame
. 您可以尝试从当前Frame
Content
属性访问Page1
对象。 Something like this : 像这样的东西:
var currentFrame = Window.Current.Content as Frame;
var page1 = currentFrame.Content as Page1;
Then public methods of Page1
will be accessible from page1
variable : 然后可以从page1
变量访问Page1
公共方法:
page1.SomePublicMethod();
Sometimes you gotta run it main thread if code is being triggered from another thread, This worked for me. 如果从另一个线程触发代码,有时你必须运行它的主线程,这对我有用。
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
try
{
ProjectsPage projectsPage = (Window.Current.Content as AppShell).AppFrame.Content as ProjectsPage;
projectsPage.FetchProjects();
}
catch (Exception ex)
{
}
});
this worked for me 这对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.