刷新/重置视图
我在 WPF 中使用 MVP,遇到了设计疑问,我想征求您的意见:
在某些时候,我需要刷新视图并执行相同的初始查询,例如加载视图时。视图的 DataContext 是我的演示者,我有几个绑定到视图的集合和其他变量。当我需要刷新视图时,我会清除集合和变量并将 DataContext 设置为 null。之后,我获取新数据、填充集合并设置 DataContext。这是实现这一目标的最佳方法吗?
这个问题是,我担心当我的应用程序变得更大时,我忘记重置某些变量......理想的情况是以某种方式重新加载视图,而不必担心我拥有的变量。
此致。
I'm using MVP in WPF and I came across a design doubt and I would to get your opinion on this:
At some point I need to refresh my view and perform the same initial queries, like when the view was loading. The view's DataContext is my presenter and I have a couple of collections and other variables that are bound to the view. When I need to refresh the view, I'm clearing the collections and the variables and setting the DataContext to null. After that I fetch new data, populate the collections and set the DataContext. Is this the best way to achieve this?
The issue with this, is that i'm affraid that when my app grows bigger I forget to reset some variable...the ideal would be to reload the view again in some way without having to worry with the variables I have.
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Jay,WPF 的设计理念是非常数据驱动的。鉴于这一事实,我建议您不需要将 DataContext 设置为 null 或重新加载视图。相反,您可以使用可观察集合或在 DataContext 对象中实现 INotifyPropertyChanged 来提醒视图正在清除和刷新数据。
Jay, the design philosophy for WPF is very data driven. Given that fact, I would suggest that you don't need to set the DataContext to null or reload the view. Instead, you could use observable collections or implement INotifyPropertyChanged in your DataContext object to alert the view to data being cleared and refreshed.