2012-01-18 88 views
1

我走了一段时间后,我用Expression Blend做了一个演示应用程序。如何使用Expression Blend动态添加/删除控件?

我的第一个屏幕是按钮的大选择,所以当用户点击任何按钮时,它会转到MainView。

然后在MainView中,我有一个菜单项列表,用户可以单击并显示其对应的DisplayView。 (Appointment Menu Item会显示AppointmentView等)。

一切都很好,我可以点击MenuItem,视图显示动画和过渡效果。

但事情是,在Expression Blend中创建MainView,Menu,AppointmentView等等每一件事物都是在XAML中预定义的。所以当用户加载第一个屏幕时必须将所有内容加载到内存中。

现在想起来了,不应该将MainView等动态添加到屏幕中吗? 如何使用Expression Blend进行操作?或者唯一的办法就是......在代码背后自己做(写StoryBoard等动态添加/删除控件?)

如果有任何示例/教程做它,它会大。

回答

0

我想你有非常有限的可能性来有条件地加载或卸载Blend中的控件,而无需编写代码隐藏。

通常,XAML中的开始标记等同于某个类对象的无参数构造函数。只要你写标签,你正在实例化一个对象,但这并不意味着它的可视化外观被加载到内存中。只有当控件实际显示在屏幕上时才会发生这种情况。

在我看来,控制某些控件外观的最简单的方法是使用单个子控件。以一个边界控件为例,并添加您想要有条件加载到其子属性的用户控件,以便决定是否加载或卸载控件。

但不幸的是我认为你必须在代码中做到这一点。拿这个简单的代码片段:

// either instantiate in code or use from markuup 
Border myBorder = new Border(); 

// the control you want to conditionally appear and disappear 
UserControl myUserControl = new UserControl(); 
myBorder.Child.Add(myUserControl); 

当然,更复杂的方法是使用网格。在这里,你必须使用附加属性来添加或删除的子元素:

// either instantiate in code or use from markuup 
Grid myGrid = new Grid(); 

// the control you want to conditionally appear and disappear 
UserControl myUserControl = new UserControl(); 

// set the target position inside the Grid via the Grids attached properties 
Grid.setRow(myUserControl, 1); 
Grid.setColumn(myUserControl, 0); 

// actually add the control 
Grid.Children.Add(myUserControl); 

虽然我敢肯定你们都知道了这一切,我希望它有助于一点:)