簡體   English   中英

如何從App.xaml.cs訪問頁面視圖

[英]How to access Page views from App.xaml.cs

我想從Application類的子類App中訪問在Page類的子類MyPage中定義的View。

sealed partial class App : Application
{
    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Here I want to access a view in MyPage class
            }
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            rootFrame.Navigate(typeof(MyPage), e.Arguments);
        }
        Window.Current.Activate();
    }
...
}

找到了辦法!

我們可以通過(MyPage)((Frame)Window.Current.Content).Content來獲取Page實例

只需在MyPage類中創建一個公共方法,然后通過以下方法在OnLaunched()方法的App類中調用它即可:

Frame rootFrame = (Frame)Window.Current.Content;
MyPage myPage = (MyPage)rootFrame.Content;
if (myPage != null)
{
    myPage.MyMethod(...);
}

暫無
暫無

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

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