簡體   English   中英

windows 8 App從App.xaml.cs訪問頁面方法

[英]windows 8 App accessing page method from App.xaml.cs

可能是一個愚蠢的問題所以我提前道歉。 我是構建Windows 8商店應用程序的新手。

當應用程序被掛起時,我需要在我的頁面腳本上運行一些方法。 我只有一個頁面,我在Page1.xaml.cs文件中有一些公共方法。 我想從App.xaml.cs文件中的OnSuspending()方法調用它們。 我需要確保保存一些文本文件。

如何創建對Page1腳本的引用?

您可以嘗試從當前Frame Content屬性訪問Page1對象。 像這樣的東西:

var currentFrame = Window.Current.Content as Frame;
var page1 = currentFrame.Content as Page1;

然后可以從page1變量訪問Page1公共方法:

page1.SomePublicMethod();

如果從另一個線程觸發代碼,有時你必須運行它的主線程,這對我有用。

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)
            {
            }
    });

這對我有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM