What is the difference between register a region t

2020-07-16 09:06发布

I want to create a region with dynamic views(multiple views in one region). The region content need to be changed by ComboBox selection event(the comobox items are view instances). I want that a change in the ComboBox will change the view in the region by the selected view item.

My question what is the difference between :

MyView view= new MyView();
IRegion region = new Region();  
region.Name="MyRegion";  
regionManager.Regions.Add(region);
region.Add(view);
region.Activate(view);

To:

regionManager.RegisterViewWithRegion("MyRegion",type(MyView));

?

What is the best way to use dynamic regions?

4条回答
Animai°情兽
2楼-- · 2020-07-16 09:37

First time i seen

type(MyView)

Maybe

typeof(MyView)

??

查看更多
看我几分像从前
3楼-- · 2020-07-16 09:45

Adding a control instance directly is called view injection. Registering a view type is known as view discovery.

Why discovery? Prism uses the current ServiceLocator the grab an instance from the Container (MEF, Unity, whatever you choose). It then adds the view automagically.

Discovery is leaner as all your views are created lazily (when they are needed). Injection lets you do some advanced compositions (like scoped regions) but you have to be more hands-on.

There is no right answer but for learning prism i would go with view discovery (RegisterViewWithRegion). For best results, use constants to hold your region names!

查看更多
该账号已被封号
4楼-- · 2020-07-16 09:53

If you want a different views to be displayed in the same region, you need to use RequestNavigate or view injection which you have used in your first method

RegisterViewWithRegion will associate the Region with the view, so that every time the control where the region is hosted become part of the visual tree the view is automatically resolved and displayed.

See the msdn entry for more information

查看更多
Ridiculous、
5楼-- · 2020-07-16 09:58

Aren't you using a container in order to inject the instances? Based on my understanding, both procedures would return the existing View if using a container with a Singleton registration configured on the related View types. You can find more information on the following MSDN Prism Guide chapter:

Regarding both implementations, RegisterViewWithRegion() method is quite similar to the first implementation: It basically loads and adds the View into the Region from the container and it activates it. So the last View registered in the Region with this method would be the active one after every initialization completes. You may check this behaviour on the PrismLibrary solution.

Like Jimmy said, the code you described would be used only for loading each Region with the corresponding Views. Then, you would use RequestNavigate() method in order to activate the selected View already registered on the Region.

You can find more related information on the following MSDN Prism Guide chapter:

I hope this helped, regards.

查看更多
登录 后发表回答