如何从类导航到xaml页面 [英] How to navigate to a xaml page from class

查看:202
本文介绍了如何从类导航到xaml页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个类Chronometer使用dispatcherTimer和Tick方法。每秒,一个变量减少。



我想这个变量的值为0时,一个新的xaml页面加载。



我尝试使用 NavigationService ,但此方法仅适用于xaml.cs文件。



我的xaml页面,当变量减少时是可见的:WordPage.xaml
我想显示的xaml页面是:FinalPage.xaml



你能实现我想要的吗?请帮助我



编辑:



我的类的构造函数是:



public Chrono(DispatcherTimer newTimer,TextBlock chrono,TextBlock NumEquip,P1 page1)
{
//零件已清除
m_page1 = page1;
}

,对象Chrono的实例是:

  MonChrono = new Chrono(newTimer,this.TimeLabel,this); 


解决方案

您不能创建 NavigationService ,这是主要的问题。但您可以将页面对象作为参数传递,并访问其 NavigationService.Property

  public void NavigateToPage(YouPageClass page)
{
page.NavigationService.Navigate(new Uri(/ Pages / PageToNavigateTo.xaml,
UriKind.RelativeOrAbsolute));
}

示例项目: http://www.abouchleih.com/wp-content/uploads/TestNavigationFromClass.zip



编辑:
我找到了一个更好的解决方案。



您的班级中的方法:

  public static void Nav(NavigationService,Uri destination)
{
s.Navigate(destination);
}

从页面调用:

  private void button_navigateTo_Click(object sender,RoutedEventArgs e)
{
NavService.Nav(this.NavigationService,new Uri(/ PageToNavigateTo.xaml, UriKind.RelativeOrAbsolute));
}


In my application, I have a class Chronometer which use a dispatcherTimer and Tick method. Each second, a variable decrease.

I would like that when the value of the variable is 0, a new xaml page is load.

I try to use NavigationService but this method work only in a xaml.cs file.

My xaml page which is visible when the variable decrease is : WordPage.xaml The xaml page that i want to display is : 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 :

MonChrono = new Chrono(newTimer, this.TimeLabel,  this);

解决方案

You cannot create an instance of NavigationService, that's the main problem. But you could possibly pass your page object as a parameter and access its 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

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.

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));
}

这篇关于如何从类导航到xaml页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆