2017-03-09 132 views
0

在项目中,我以这种方式显示Window访问页面的功能

MainWindow mainWindow = new MainWindow(); 
mainWindow.Show(); 

比我在自己的框架加载Page

mainWindow.frame.NavigationService.Navigate(new Uri("PageWelcome.xaml", UriKind.Relative)); 

PageWelcome包含一些公共功能如下:

public void Play() 
{ 
    mediaElement.Play(); 
} 

现在来自上面的初始类,我想调用Play方法。 这样做的正确方法是什么? 我必须检索框架的当前内容?

+0

这个解决方案有什么问题吗? '((PageWelcome)mainWindow.frame.Content).Play();' – Mark

回答

1

您既可以在FrameContent财产转换为PageWelcome

PageWelcome page = mainWindow.frame.Content as PageWelcome; 
if(page != null) 
    page.Play(); 

或者您也可以创建Page的情况下,保持在你的类的字段对它的引用,并设置FrameContent属性:

private WelcomePage page = new WelcomePage(); 
public void SomeMethod() 
{ 
    MainWindow mainWindow = new MainWindow(); 
    mainWindow.frame.Content = page; 
    mainWindow.Show(); 
} 

private void SomeOtherMethod() 
{ 
    page.Play(); 
} 

使用你不必投了后一种方式。