是什么决定NavigationCommands.BrowseBack是否调用页面的构造函数? [英] What determines whether NavigationCommands.BrowseBack calls the page constructor?

查看:285
本文介绍了是什么决定NavigationCommands.BrowseBack是否调用页面的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两页在他们类似的逻辑。加载页面,点击一些按钮,将显示/隐藏其他按钮,继续下一个页面。当我打的下一个页面,如果我点击后退按钮,我返回到前一页。



不同的是,一个页面(第一页),将有当我点击后退按钮,其中有一个电话重置默认称为构造函数。其他页面(SecondPage)不会被调用构造函数,我不知道为什么。

 公开第一页()
{
的InitializeComponent();
DisplayStuff();
}



第一页有的KeepAlive 设置为

 公共SecondPage(对象ARG1,对象ARG2) 
{
的InitializeComponent();
DisplayStuff(ARG1,ARG2);
}

本页面还包含的KeepAlive 设置为。这两个页面没有任何来自继承并没有什么重写任何属性。我可以看到的唯一区别是空的构造函数,所以我想给SecondPage一个空的构造,仍然没有运气。



我是比较新的WPF(我的工作它一两个小时每6个月),所以我在想什么?



下面是万一后退按钮它是相关的。

 <按钮命令={X:静态NavigationCommands.BrowseBack}/> 



编辑:当我点击后退按钮,SecondPage不保留其状态。它只是加载了一个空白页面,因为 DisplayStuff 还没有被称为尚未



导航代码:

 的NavigateTo(新SecondPage(ARG1,ARG2)); 

保护无效的NavigateTo(页页)
{
NavigationService.Navigate(页);
}


解决方案

我创建了一个类似的示例应用程序并有类似的行为。我想什么了,当你回到一个页面的构造不叫,除非该页面在日记的第一页



阅读的导航在WPF




在页面页面导航回,采用了日记,以下步骤进行:




  1. 页(背面堆栈顶部的日记帐分录)被实例化。


  2. 页面刷新与已储存的日记帐分录状态页。


  3. 在页面导航回。





祝你好运!


I have two pages with similar logic in them. Load the page, click some buttons that will show/hide other buttons, continue to next page. When I hit the next page, if I click the back button I am returned to the previous page.

The difference is that one page (FirstPage) will have the constructor called when I click the back button, which has a call to reset the defaults. The other page (SecondPage) doesn't get the constructor called and I'm not sure why.

public FirstPage()
{
  InitializeComponent();
  DisplayStuff();
}

FirstPage has KeepAlive set to False.

public SecondPage(object arg1, object arg2)
{
  InitializeComponent();
  DisplayStuff(arg1, arg2);
}

This page also has KeepAlive set to False. These two pages don't inherit from anything and there is nothing that overrides any of the properties. The only difference I can see is the empty constructor, so I tried giving SecondPage an empty constructor and still no luck.

I'm relatively new to WPF (I work on it for an hour or two every 6 months), so what am I missing?

Here is the back button in case it is relevant.

<Button Command="{x:Static NavigationCommands.BrowseBack}" />

Edit: When I click the back button, SecondPage doesn't keep its state. It just loads an empty page because DisplayStuff hasn't been called yet.

Navigation Code:

NavigateTo(new SecondPage(arg1, arg2));

protected void NavigateTo(Page page)
{
  NavigationService.Navigate(page);
}

解决方案

I created a similar sample application and had similar behaviour. What I figured out that when you go back to a page the constructor is not called unless the page is the first page in the journal

Read this section in Navigation in WPF:

When the page Page is navigated back to, using the journal, the following steps take place:

  1. The Page (the top journal entry on the back stack) is instantiated.

  2. The Page is refreshed with the state that was stored with the journal entry for the Page.

  3. The Page is navigated back to.

Good luck!

这篇关于是什么决定NavigationCommands.BrowseBack是否调用页面的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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