tags:

views:

34

answers:

1

In my ViewModel, I want to build a collection of Page objects from this list of page names:

private string[] pageNames = {
    "Introduction.xaml",
    "Slide1.xaml",
    "Slide2.xaml"
};

How do I instantiate them dynamically, e.g. something like this:

foreach (string pageName in pageNames)
{
    //PSEUDO CODE:
    Page thePage = new &&pageName();
    thePages.Add(thePage);

}
A: 

You can use XamlReader.Load :

foreach (string pageName in pageNames)
{
    string xaml = File.ReadAllText(pageName);
    Page thePage = XamlReader.Load(xaml);
    thePages.Add(thePage);
}

(I'm not sure about the File.ReadAllText, it depends where your files are...)

Thomas Levesque