在复合WPF(Prism)中,IRegion.Add和IRegionManager.RegisterViewWithRegion有什么区别?

发布于 2024-08-04 06:27:19 字数 530 浏览 6 评论 0原文

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

IRegion.Add

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

IRegionManager.RegisterViewWithRegion

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

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 ) );
}

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

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

发布评论

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

评论(2

ま昔日黯然 2024-08-11 06:27:19

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

由于一些技术原因,您可能想要选择其中之一。例如,

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

相关文档是:
对于视图组合(包括视图注入与视图发现以及视图优先或视图呈现者优先方法的讨论):
http://msdn.microsoft.com/en-us/library/dd458944.aspx

还有一个非常方便的“何时使用每个”部分。这是文档的摘录:

  • 对何时创建视图进行显式或编程控制
    显示,或者当您需要时
    从区域中删除视图,对于
    例如,作为应用的结果
    逻辑。
  • 要将同一视图的多个实例显示到一个区域中,其中
    每个视图实例都绑定到
    不同的数据。
  • 控制添加视图的区域实例(例如
    例如,如果你想添加
    客户详细信息视图
    客户详细信息区域)。注意
    此场景需要范围
    稍后描述的区域
    主题。

希望这有帮助。

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

  • you had a more complicated way of creating views (maybe you want to create the View and its ViewModel and marry them by setting the DataContext property yourself), you'd need to use View Injection
  • if you take advantage of Region Scopes, you will be forced to use View Injection.

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:

  • Explicit or programmatic control over when a view is created and
    displayed, or when you need to
    remove a view from a region, for
    example, as a result of application
    logic.
  • To display multiple instances of the same views into a region, where
    each view instance is bound to
    different data.
  • To control which instance of a region a view is added (for
    example, if you want to add
    customer detail view to a specific
    customer detail region). Note that
    this scenario requires scoped
    regions described later in this
    topic.

Hope this helps.

浪菊怪哟 2024-08-11 06:27:19

RegisterViewWithRegion 引发 OnContentRegistered 事件,但当然情况可能并非如此,具体取决于您的 DI

RegisterViewWithRegion raises the OnContentRegistered event, but of course that could not be the case depending on your DI

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