如何使用 Expression Blend 动态添加/删除控件?
有一段时间,我使用 Expression Blend 制作了一个演示应用程序。
我的第一个屏幕是大量按钮选择,因此当用户单击任何按钮时,它都会转到主视图。
然后在 MainView 中,我有一个菜单项列表,用户可以单击该菜单项并显示其相应的 DisplayView。 (约会菜单项将显示 AppointmentView 等)。
一切都很好,我可以单击菜单项,视图会显示动画和过渡效果。
但问题是,在 Expression Blend 中创建时,MainView、Menu、AppointmentView 等所有内容都是在 XAML 中预定义的。因此,当用户加载第一个屏幕时,必须将所有内容加载到内存中。
现在想起来,MainView等不应该动态添加到屏幕中吗? 如何使用 Expression Blend 做到这一点?或者唯一的方法就是......在我自己的代码隐藏中进行(为动态添加/删除控件编写故事板等?)
如果有任何示例/教程,那就太好了。
A while I go, I made a demo application with Expression Blend.
My first screen is a big selections of Buttons, so when user click on any of button, it goes to the MainView.
Then in the MainView, I have a list of Menu items that user can click and shows up its corresponing DisplayView. (Appointment Menu Item will shows up AppointmentView etc).
Everything is good, I can click the MenuItem, the Views shows up with animation and transition effects.
But the thing is, with creating in Expression Blend, the MainView, Menu, AppointmentView etc every thing is predefined in the XAML. So when user load the first screen has to load everything into memory.
Now thinking of it, shouldn't the MainView etc be dynamically add into the screen?
How do I do it with Expression Blend? Or the only way to do is just....do it in code-behind myself (writting StoryBoard etc for the dynamic add/remove controls?)
If there is any example/tutorial of doing it, it will be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您在 Blend 中专门有条件地加载或卸载控件而无需编写隐藏代码的可能性非常有限。
一般来说,XAML 中的开始标记相当于某个类对象的无参数构造函数。一旦您编写了标签,您就正在实例化一个对象,但这并不意味着它的视觉外观已加载到内存中。仅当控件实际显示在屏幕上时才会发生这种情况。
在我看来,控制某些控件的外观的最精简方法是使用单子控件。以边框控件为例,将要有条件加载的用户控件添加到其子属性中,以便您可以决定是否加载或卸载控件。
但不幸的是我认为你也必须在代码中做到这一点。看看这个简单的代码片段:
当然,更复杂的方法是使用网格。在这里,您必须使用附加属性来添加或删除子元素:
虽然我很确定您知道所有这些,但我希望它能有所帮助:)
I guess you have very limited possibilities to conditionally load or unload controls exclusively in Blend without writing code-behind.
In general an opening tag in XAML is equivalent to a parameter-less constructor of some class object. As soon as you write the tags your are instantiating an object but that doesn't mean that it's visual appearance is loaded into memory. This only happens when the control is actually shown on the screen.
In my opinion the leanest way to control the appearance of some control is to use a single-child control. Take a Border control for example and add the user control you want to conditionally load to its child property, so you can decide for example whether to load or unload a control.
But unfortunately I think you have to do this in code as well. Take this easy code snippet:
Of course a much more sophisticated approach is to use Grids. Here you have to use attached properties to add or remove child elements:
Although I am pretty sure you were aware of all of that I am hoping it helped a bit :)