windows 8 App accessing page method from App.xaml.cs

拟墨画扇 提交于 2019-12-10 17:39:12

问题


Probably a stupid question so I apologise in advance. I am new to building a Windows 8 Store App.

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. I want to call them from the OnSuspending() method in the App.xaml.cs file. I need to make sure that some of the text files are saved.

How can I create a reference to my Page1 script?


回答1:


You can try accessing Page1 object from Content property of current Frame. 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.SomePublicMethod();



回答2:


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



来源:https://stackoverflow.com/questions/21419875/windows-8-app-accessing-page-method-from-app-xaml-cs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!