如何在导航wpf应用程序中保持页面完整状态
我正在使用页面和导航服务构建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要告诉主机应用程序该页面应该保留在内存中,而不是每次导航离开时“卸载”并在返回时“重新加载”。 事实证明这非常简单:只需添加 页面声明的 KeepAlive 属性:
有趣的是,MSDN 文档是这样说的:
我没有发现这种情况,从你的问题来看,你似乎也没有发现这种情况。
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:
Interestingly, the MSDN documentation says this:
I've not found that to be the case, and from your question it seems you're not finding it that way either.