开发者

Dynamically generate XAML objects

I have a Silverlight app that displays a number of "pages". Each page is a different XAML file with different code behind. The pages are numbered sequentially as follows: page_1, page_2, page_3, ..., Page_n. The pages are not static and will be dynamically generated.

Since I don't know the total number of pages, I have to load each page at runtime using the Dynamic keyword. My code is as follows as is working perfectly:

Type type = Type.GetType("Pages.Page_" + (index).ToString(), true);
dynamic newPage = Activator.CreateInstance(type);

My problem is that I've just learned that the app must be Silverlight 3 and, as a result, it will not be able to use the dynamic type. I've tried changing "dynamic" to "object" but I need to be able to access the XAML on each page and manipulate the XAML. If all I needed was to access pr开发者_开发知识库operties and methods, I'd be able to follow the solution for creating dynamic objects here.

How can I dynamically create each page and still be able to access the XAML?


I'm assuming that each page is a UserControl. If this is the case then you're pretty much already there. Instead of creating dynamic objects, create a bunch of UserControl objects.

Change your code to this:

Type type = Type.GetType("Pages.Page_" + (index).ToString(), true);
UserControl newPage = (UserControl)Activator.CreateInstance(type);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜