如何在导航wpf应用程序中保持页面完整状态

发布于 2024-07-19 03:22:01 字数 587 浏览 3 评论 0原文

我正在使用页面和导航服务构建 WPF 应用程序。
其中一个页面将一个对象作为构造函数

Sub New(ByVal o As Object)
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ....

因此,要导航到它,我会这样做

    Dim MyPage As New Page1(MyObject)
    MyBase.NavigationService.Navigate(MyPage)

当我在页面中编辑某些内容,然后返回并转发到 MyPage 时,会出现以下错误:

 Cannot create object of type 'Page1'. CreateInstance failed, which can be 
 caused by not having a public default constructor for 'Page1'.  

什么我做错了吗?

I'm building a WPF app using pages and the navigation service.
One of the pages take an object as a constructor

Sub New(ByVal o As Object)
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ....

So, to navigate to it I do

    Dim MyPage As New Page1(MyObject)
    MyBase.NavigationService.Navigate(MyPage)

The problem occurs when I'm editing something in the page, and go back, and the forward to MyPage i get the following error:

 Cannot create object of type 'Page1'. CreateInstance failed, which can be 
 caused by not having a public default constructor for 'Page1'.  

What am I doing wrong?

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

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

发布评论

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

评论(1

纸短情长 2024-07-26 03:22:01

您需要告诉主机应用程序该页面应该保留在内存中,而不是每次导航离开时“卸载”并在返回时“重新加载”。 事实证明这非常简单:只需添加 页面声明的 KeepAlive 属性:

<Page x:Class="..." KeepAlive="True">

有趣的是,MSDN 文档是这样说的:

实例化的页面
导航到仅使用代码(对于
例如,调用 Navigate),是
自动保持活动状态。

我没有发现这种情况,从你的问题来看,你似乎也没有发现这种情况。

You need to tell the host application that the page should persist in memory, rather than being "unloaded" every time you navigate away and "reloaded" when you come back to it. That turns out to be pretty easy: Just add the KeepAlive attribute to your page declaration:

<Page x:Class="..." KeepAlive="True">

Interestingly, the MSDN documentation says this:

Pages that are instantiated and
navigated to using only code (for
example, calling Navigate), are
automatically kept alive.

I've not found that to be the case, and from your question it seems you're not finding it that way either.

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