我想从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();
}
...
}
答案 0 :(得分:0)
找到了办法!
我们可以通过(MyPage)((Frame)Window.Current.Content)获取Page实例。内容
只需在MyPage类中创建一个公共方法,然后通过OnLaunched()方法从App类中调用它:
Frame rootFrame = (Frame)Window.Current.Content;
MyPage myPage = (MyPage)rootFrame.Content;
if (myPage != null)
{
myPage.MyMethod(...);
}