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

查看:20
本文介绍了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();

我想要这样的东西

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).

推荐答案

假设您的意思是您从数据库中获取要确定要显示的页面名称的页面的名称.

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.

我将采用最简单的示例,其中所有页面都位于单个应用程序程序集中和单个已知命名空间中.它可以像这样简单:-

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);

也许更灵活的方法是在数据库中存储一个 AssemblyQualifiedName.这样页面就可以在不同的程序集和/或命名空间中,它只需要出现在 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天全站免登陆