本文介绍了在 Composite WPF (Prism) 中,IRegion.Add 和 IRegionManager.RegisterViewWithRegion 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Composite WPF (Prism)中,向IRegionManger集合添加模块时,使用IRegion有什么区别.Add 和 IRegionManager.RegisterViewWithRegion?

In Composite WPF (Prism), when adding modules to the IRegionManger collection, what is the difference between using IRegion.Add and IRegionManager.RegisterViewWithRegion?

IRegion.Add

public void Initialize()
{
    _regionManager.Regions["MainRegion"].Add( new ModuleAView() );
}

IRegionManager.RegisterViewWithRegion

public void Initialize()
{
    _regionManager.RegisterViewWithRegion( "MainRegion", typeof( ModuleAView ) );
}

推荐答案

区别在于谁负责创建视图.在IRegion.Add 场景(也称为视图注入)中,您负责预先实例化视图.在另一个使用 RegisterViewWithRegion(也称为 View Discovery)的场景中,区域管理器实例化视图本身.

The difference is who is responsible for creating the view. In the IRegion.Add scenario (also called View Injection) you are responsible for instantiating the view beforehand. In the other scenario with RegisterViewWithRegion (also called View Discovery), the region manager instantiates the view itself.

出于某些技术原因,您可能希望这样做.例如

There are some technical reasons you would want to do one or the other. For example

  • 您有一种更复杂的创建视图的方法(也许您想创建 View 及其 ViewModel 并通过自己设置 DataContext 属性将它们结合起来),您需要使用 View Injection
  • 如果您利用区域范围,您将被迫使用视图注入.

相关文件是:对于 View Composition(包括 View Injection vs. View Discovery 以及 View-First 或 View-Presenter-First 方法的讨论):http://msdn.microsoft.com/en-us/library/dd458944.aspx

The relevant documenation is:For View Composition (including View Injection vs. View Discovery and discussions of View-First or View-Presenter-First approaches):http://msdn.microsoft.com/en-us/library/dd458944.aspx

还有一个非常方便的何时使用每个"部分.以下是文档的摘录:

There's also a really handy "when to use each" section. Here's the excerpt from the docs:

  • 对创建视图的时间进行显式或编程控制,以及显示,或当您需要从区域中删除视图,对于例如,作为应用的结果逻辑.
  • 要将相同视图的多个实例显示到一个区域中,其中每个视图实例都绑定到不同的数据.
  • 控制添加视图的区域实例(对于例如,如果您想添加客户详细信息查看特定客户详细信息区域).注意这种情况需要作用域本部分稍后描述的区域主题.

希望这会有所帮助.

这篇关于在 Composite WPF (Prism) 中,IRegion.Add 和 IRegionManager.RegisterViewWithRegion 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:39