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

Edu*_*eni 4 wpf navigationservice

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

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

    ....
Run Code Online (Sandbox Code Playgroud)

所以,为了导航它,我做到了

    Dim MyPage As New Page1(MyObject)
    MyBase.NavigationService.Navigate(MyPage)
Run Code Online (Sandbox Code Playgroud)

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

 Cannot create object of type 'Page1'. CreateInstance failed, which can be 
 caused by not having a public default constructor for 'Page1'.  
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Mat*_*ton 6

您需要告诉主机应用程序该页面应该保留在内存中,而不是每次导航时都"卸载"并在您返回时"重新加载".事实证明这很简单:只需将KeepAlive属性添加到页面声明中:

<Page x:Class="..." KeepAlive="True">
Run Code Online (Sandbox Code Playgroud)

有趣的是,MSDN文档说明了这一点:

实例化并仅使用代码导航到的页面(例如,调用Navigate)会自动保持活动状态.

我没有发现情况如此,从你的问题来看,你似乎也没有找到它.