user1766857
user1766857

Reputation: 43

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.

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?

Upvotes: 4

Views: 817

Answers (2)

JD-V
JD-V

Reputation: 3676

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

Upvotes: 0

har07
har07

Reputation: 89305

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();

Upvotes: 4

Related Questions