Nam G VU
Nam G VU

Reputation: 35384

How to check if a view already added to a region in PRISM?

I have a region and want to check whether a specific view type is added to it or not. How can I do it?

Upvotes: 1

Views: 3338

Answers (3)

Punit Kumar
Punit Kumar

Reputation: 1

object obj = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(cntrlName);

var checkIfAlreadyExists = 
RegionManager.Regions["ApplicationCoreRegion"].Views.Contains(obj);

if (checkIfAlreadyExists) {
    MessageBox.Show("Can not add this, because it is already shown");
} else {
    RegionManager.RegisterViewWithRegion("ApplicationCoreRegion", () => obj);
    RegionManager.Regions["ApplicationCoreRegion"].Activate(obj);
}

Upvotes: 0

Damian Schenkelman
Damian Schenkelman

Reputation: 3535

The following code (using Linq) should be useful:

regionManager.Regions["RegionName"].Views.Any(v => v.GetType() == typeof(ViewType));

Hope this helps,

Upvotes: 4

Damian
Damian

Reputation: 1553

You can check to see if a view has been added to a region using the following method.

var regionManager = Get reference to the region manager
bool viewHasBeenAdded = regionManager.Regions["Your region"].GetView("View Name") != null;

Is this what you want or are you actualy wanting to check for Type rather than View name?

Upvotes: 2

Related Questions