In Composite WPF (Prism), what is the difference between IRegion.Add and IRegionManager.RegisterViewWithRegion?

前端 未结 2 586
醉梦人生
醉梦人生 2021-02-07 13:07

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

<
相关标签:
2条回答
  • 2021-02-07 13:22

    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.

    0 讨论(0)
  • 2021-02-07 13:22

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

    0 讨论(0)
提交回复
热议问题