Darf Zon
Darf Zon

Reputation: 6378

How to get the view from a region in PRISM?

I'm using PRISM and I'm trying to get the view, but returns null the function GetView().

What am I doing wrong?

    public void Initialize()
    {
        _regionManager.RegisterViewWithRegion("TopLeftRegion", () => _container.Resolve<View1>());
        _container.RegisterType<Object, View1>("ViewB");

        var view = _regionManager.Regions["TopLeftRegion"].GetView("ViewB");
    }

Upvotes: 3

Views: 3320

Answers (1)

Bahri Gungor
Bahri Gungor

Reputation: 2319

The reason it is returning null is because there are no views with the name "ViewB". When you use RegisterViewWithRegion, Prism activates a new instance of the view type (in your case View1>. However, there is no way to name that instance for the views collection using that technique.

To accomplish what you want to do, you need to add a view that you instantiate yourself to the region.

_regionManager.Regions["TopLeftRegion"].Add(new View1(),"ViewB");

var view = _regionManager.Regions["TopLeftRegion"].GetView("ViewB");

More information can be found here

Upvotes: 5

Related Questions