Silverlight 4 + Prism 在运行时创建控件
我正在创建一个 Silverlight 4 应用程序来管理活动目录对象(CRUD 用户、组等)。现在我们都知道活动目录对象有很多属性。 我想在 SQL DB 中保存某些 AD 对象类型(用户、组等)的属性名称组(即 samaccountname、mail 等)。 当silverlight应用程序中的用户单击“创建用户”时,我想
- 获取我在sql数据库中为用户定义的所有属性名称,
- 为每个属性创建一个预定义的silverlight控件并将其添加到当前的“创建视图”
- 中能够读取用户在控件中输入的所有值并将其传递。
用户控件根据 Active Directory 属性的类型而有所不同,即对于具有日期时间值的属性,我们将创建一个带有日历的用户控件。这样我们就可以保持“创建”视图的动态。
Silverlight / Prism 基础已经存在,我正在通过 RegionManager 添加我的视图。一切都工作得很好,但我不确定如何以动态方式添加控件,使其仍然可以与 PRISM 和 MVVM 模式一起使用。感谢您的任何意见。
I'm creating a Silverlight 4 application to manage active directory objects (CRUD Users, groups etc). Now we all know that active directory objects have lots of properties.
I want to save some groups of property names in an SQL DB (ie, samaccountname, mail, etc.) for certain AD object types (users, groups, etc).
When a user in the silverlight app clicks on "create user" I want to
- get all the property names I defined in the sql db for the user
- create a pre defined silverlight control for each property and add it to the current "create view"
- be able to read all the values the user enters in the controls and pass them on
The user controls differ depending on the type of the Active Directory property, i.e. for a property with a datetime value, we will create a user control with a calendar. This way we can keep the "Create" Views dynamic.
The Silverlight / Prism foundation is already there and I'm adding my Views via the RegionManager. It all works just fine but I'm not sure how to add controls in such a dynamic way that it still works with PRISM and the MVVM pattern. Thank you for any input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您熟悉从数据库获取属性并将值保存回来,并专注于使用 MVVM 创建视图的中间区域。
棱镜部分我不担心。您说您已向区域经理注册了视图,这就是您需要做的全部事情。
我最初的想法是如何在考虑 MVVM 的情况下在运行时“创建”控件。创建控件并不是困难的部分,但需要对将要注入这些控件的元素进行句柄,例如根网格。从表面上看,这感觉非常不符合 MVVM。就我个人而言,我认为以旧方式完成“视图”代码是有效的,绑定中使用的“数据”代码应该是 MVVM 的。问题是告诉代码将控件注入到视图中实际注入什么。我想不出一个巧妙的方法来做到这一点。
作为解决方案,您是否可以创建一个包含适用于每个属性的所有控件的视图,而不是在需要时“创建”它们,在不需要时“隐藏”它们?您可以将“Visibility”属性(可能通过值转换器)绑定到 ViewModel 中的一个属性,该属性可用于确定是否应显示该特定控件。如果将它们放置在堆栈面板中,则视图将动态缩小。
如果您认为代码示例有帮助,请告诉我。
I'm going to assume that your comfortable with the getting the properties from the db and saving the values back and focus on the middle area of creating the view with MVVM in mind.
The PRISM part I wouldn't worry about. You say you've got the views registered with the region manager and that's about all you need to do.
My initial thought was how would you "create" controls at runtime with MVVM considered. Creating controls is not the hard part but would require a handle on the element you will be injecting these into, a root grid for example. This would on the surface feel very un-MVVM. Personally I think it's valid to have "view" code done the old fashion way, it's the "data" code used in binding that should be MVVM'd. The issue is telling the code that will inject the controls on to the view what to actually inject. I can't think of a neat way to do this.
As a solution could you not create a view containing all the controls that apply to each of the properties and rather than "create" them when needed "hide" them when not needed? You could bind the "Visibility" property (probably through a value converter) to a property in the ViewModel that can be used to determine if that particular control should be shown or not. If these where placed in a stack panel then the view would dynamically shrink.
If you think a code example would help let me know.