C# 如何从App.xaml.cs访问页面视图

C# 如何从App.xaml.cs访问页面视图,c#,windows,C#,Windows,我想从应用程序类的子类App访问在页面类的子类MyPage中定义的视图 sealed partial class App : Application { public App() { this.InitializeComponent(); this.Suspending += OnSuspending; } protected override void OnLaunched(LaunchActivatedEventArgs e)

我想从应用程序类的子类App访问在页面类的子类MyPage中定义的视图

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)获取页面实例

只需在MyPage类中创建一个公共方法,并通过以下方式从OnLaunched()方法中的App类调用它:

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

为什么要从App.cs访问它?因为我需要在OnLaunched()方法中更改TexBox视图的文本值,该值可在App类中进行重写(因为它继承了应用程序类)