是什么决定了 NavigationCommands.BrowseBack 是否调用页面构造函数?

发布于 2024-11-01 01:32:11 字数 1004 浏览 6 评论 0原文

我有两个具有相似逻辑的页面。加载页面,单击一些按钮将显示/隐藏其他按钮,继续下一页。当我点击下一页时,如果单击后退按钮,我将返回到上一页。

不同之处在于,当我单击后退按钮时,一个页面(FirstPage)将调用构造函数,该构造函数会调用重置默认值。另一个页面(SecondPage)没有调用构造函数,我不知道为什么。

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

FirstPage 将 KeepAlive 设置为 False

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

此页面还将 KeepAlive 设置为 False。这两个页面不继承任何内容,也没有任何内容覆盖任何属性。我能看到的唯一区别是空的构造函数,所以我尝试给 SecondPage 一个空的构造函数,但仍然没有成功。

我对 WPF 比较陌生(我每 6 个月会花一两个小时),那么我错过了什么?

这是后退按钮,以防相关。

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

编辑:当我单击后退按钮时,SecondPage 不会保持其状态。它只是加载一个空页面,因为尚未调用 DisplayStuff

导航代码:

NavigateTo(new SecondPage(arg1, arg2));

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弱骨蛰伏 2024-11-08 01:32:11

我创建了一个类似的示例应用程序并具有类似的行为。我发现,当您返回到某个页面时,除非该页面是期刊中的第一页,否则不会调用构造函数

阅读 WPF 中的导航

当使用日志导航回页面 Page 时,会发生以下步骤:

  1. 页面(返回堆栈上的顶部日志条目)已实例化。

  2. 页面将刷新为与页面的日记条目一起存储的状态。

  3. 页面导航回。

祝你好运!

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!

淤浪 2024-11-08 01:32:11

阅读 Paul Stovell 关于 WPF 导航的文章后,我想要的显示内容的方式行不通。

导航时,如果单击“后退”,WPF 不可能知道要传递给构造函数的值;因此它必须保持页面处于活动状态。这是跟踪输出:

由于 WPF 无法调用构造函数,因此它不会。它只会让页面保持活动状态。

他接着提到,如果您不通过 URI 导航,KeepAlive 将不起作用,并且每次都会调用 LoadedUnloaded,所以我可以将所有逻辑移到那里,并且不需要在后导航上调用构造函数。

After reading Paul Stovell's article on WPF navigation, the way I want to display stuff is not going to work.

When navigating, if you click "Back", WPF can't possibly know what values to pass to the constructor; therefore it must keep the page alive. Here's the trace output:

Since WPF can't call the constructor, it won't. It'll just keep the page alive.

He goes on to mention that KeepAlive doesn't work if you're not navigating via URI, and Loaded and Unloaded are called each time, so I can just move all my logic there and I won't need the constructor to be called on the back navigation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文