Search code examples
c#windows-8

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?


Solution

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