Silverlight的:如何动态地创建一个页面 [英] Silverlight: How to create a page dynamically

查看:100
本文介绍了Silverlight的:如何动态地创建一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我保存,我想在Silverlight在数据库打开的页面名称。当我启动的应用程序,我想将页面设置为这个字符串

Problem: I store the page name I want opened in Silverlight in a database. When I startup the application I want to set the page to this string

所以,而不是这样的:

this.RootVisual = new MainPage();



我想是这样的。

I want something like this

string pageName = getValueFromDatabase()
if (!PageExists(pageName))
   throw error
else
   this.RootVisual = SomeWizzyMethodToCreatePage(pageName) 

我想我需要在这里使用反射来找到所有的页面(PageExists),然后以某种方式创建一个新的实例(SomeWizzyMethodToCreatePage)。

I guess I will need to use reflection here to find all of the pages (PageExists), and then somehow create a new instance (SomeWizzyMethodToCreatePage).

推荐答案

假设你的意思是,你从数据库AQUIRE在名称要确定页面中显示的名称的页面。

Assuming you mean the you aquire from the DB the name of the page that you want to determine the name of the page to display.

我要在所有的页面都在一个单一的应用程序集最简单的例子,并一种已知的命名空间。它可以像这样简单: - 。SilverlightApplication1

I'll take the simplest example where all the pages are in a single application assembly and a single known namespace. It can be as simple as this:-

Type pageType = Assembly.GetExecutingAssembly().GetType("SilverlightApplication1." + pageName);
RootVisual = (UIElement)Activator.CreateInstance(pageType);



也许更flexibable的方法是在数据库中的 AssemblyQualifiedName <存储/ code>。这样的页面可以在不同的装配和/或命名空间,因此只需要出现在XAP(我不知道它是否能成为一个缓存组件库ZIP)。如果页面名称是 AssemblyQualifiedName 然后代码变为: -

Perhaps a more flexibable approach would be to store in the database an AssemblyQualifiedName. That way the page can be in a different assembly and/or namespace, it need only be present in the XAP (I'm not sure whether it can be in a cached assembly library zip). If the page name is an AssemblyQualifiedName then the code becomes:-

Type pageType = Type.GetType(pageName);
RootVisual = (UIElement)Activator.CreateInstance(pageType);

这篇关于Silverlight的:如何动态地创建一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆