[英]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.