how to get list of ....

Topics: Prism v4 - WPF 4
Dec 4, 2011 at 1:06 PM

1) Is there a way to get a list of View instances in the app preferably at the window level in cases where the app has multiple windows

2) is there a guid/unique id associated with each view instance within the prism infrastructure ( this is needed to associate to some custom state information with the instance id and would like to reuse it if possible )

 

thanks

Developer
Dec 5, 2011 at 6:49 PM
Edited Dec 5, 2011 at 6:50 PM

Hi,

Based on my understanding Prism doesn't provide a guid/unique id associated with each view from out the box. On the other hand, you might benefit from Prism regions to obtain the list of views inside your application. One way could be accessing all the collections of views in all regions for your region manager. This can be done from any place if you have access to the RegionManager. An example of this could be like in the following code snippet:

 

var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

var views = new List<object>();

foreach (var region in regionManager.Regions)
	{
	foreach (var view in region.Views)
	{
	views.Add(view);
	}
}

 

Also you could try creating an extension method for the IRegionManager interface to perform the aforementioned operation (note that this won’t bring views from regions inside other region managers i.e. scoped region managers). An example could be like in the following code snippet:

 

public static IList<object> GetAllViews(this IRegionManager regionManager)
{
	var views = new List<object>();

	foreach (var region in regionManager.Regions)
	{
	foreach (var view in region.Views)
		{
		views.Add(view);
		}
	}

return views;
}

 

Also, if you need to retrieve a particular view in a region you could call the GetView(String viewName) method if your view was added to the region with a viewName.
On the other hand you might benefit from the Object.GetHashCode Method if you want to uniquely identify a particular instance of a view throughout the application.

For example you could add all your views in a Dictionary with their corresponding hash codes as the key value:

 

public static IDictionary<int,object> GetAllViews(this IRegionManager regionManager)
{
var views = new Dictionary<int,object>();

foreach (var region in regionManager.Regions) { foreach (var view in region.Views) { views.Add(view.GetHashCode(),view); } } return views; }

 

You can find more information about this topic in the Prism Documentation at MSDN:

I hope you find this helpful

Agustin Adami
http://blogs.southworks.net/aadami