[英]How to navigate to a xaml page from class
In my application, I have a class Chronometer which use a dispatcherTimer and Tick method. 在我的应用程序中,我有一个计时表类,它使用dispatcherTimer和Tick方法。 Each second, a variable decrease.
每秒,变量减少。
I would like that when the value of the variable is 0, a new xaml page is load. 我希望当变量的值为0时,将加载一个新的xaml页面。
I try to use NavigationService
but this method work only in a xaml.cs file. 我尝试使用
NavigationService
但是此方法仅在xaml.cs文件中有效。
My xaml page which is visible when the variable decrease is : WordPage.xaml The xaml page that i want to display is : FinalPage.xaml 当变量减少时可见的我的xaml页面:WordPage.xaml我要显示的xaml页面是:FinalPage.xaml
Can you realize what I want? 你知道我想要什么吗? Please help me
请帮我
EDIT : 编辑:
My constructor of my class is : 我的班级的构造函数是:
public Chrono(DispatcherTimer newTimer, TextBlock chrono, TextBlock NumEquip, P1 page1)
{
//Part effaced
m_page1 = page1;
}
and the instanciation of the object Chrono is : 而Chrono对象的实例化为:
MonChrono = new Chrono(newTimer, this.TimeLabel, this);
You cannot create an instance of NavigationService , that's the main problem. 您不能创建NavigationService的实例,这是主要问题。 But you could possibly pass your page object as a parameter and access its
NavigationService.Property
但是您可以将页面对象作为参数传递并访问其
NavigationService.Property
public void NavigateToPage(YouPageClass page)
{
page.NavigationService.Navigate(new Uri("/Pages/PageToNavigateTo.xaml",
UriKind.RelativeOrAbsolute));
}
Sample project: http://www.abouchleih.com/wp-content/uploads/TestNavigationFromClass.zip 示例项目: http : //www.abouchleih.com/wp-content/uploads/TestNavigationFromClass.zip
Edit: I found a better solution. 编辑:我找到了一个更好的解决方案。 Don't pass the page object, bette pass it's NavigationService-Property, now you can simply call the same method from multiple pages.
不要传递页面对象,而是传递它的NavigationService-Property,现在您可以简单地从多个页面调用相同的方法。
Method in your class: 您班上的方法:
public static void Nav(NavigationService s, Uri destination)
{
s.Navigate(destination);
}
Call from a page: 从页面致电:
private void button_navigateTo_Click(object sender, RoutedEventArgs e)
{
NavService.Nav(this.NavigationService, new Uri("/PageToNavigateTo.xaml", UriKind.RelativeOrAbsolute));
}
在App.xaml中使用RootFrame
(App.Current as App).RootFrame.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.