How to dynamically create page objects from a collection of page names (strings)?

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 to create them dynamically eg. something like that:

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

}

      

0


a source to share


1 answer


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 file, it depends where your files are ...)

0


a source







All Articles