工具栏在企业应用系统中是比较常见的导航工具。XCenter中的工具栏写的并不精彩,这里只是抛砖引玉。欢迎大家有更好的解决方案时与我讨论。
XCenter中的工具栏又两个位于XCenter.Framework.Client.Controls命名空间中的UserControl实现:ToolBar.xaml,ToolbarButton.xaml。ToolbarButton是工具栏上的按钮,ToolBar是容器。
先看看界面效果:
ToolbarButton XAML代码如下:
<Button x:Class="XCenter.Framework.Client.ToolbarButton"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
4 <Button.Resources>
5 <Storyboard x:Name="Activate">
6 <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2" Storyboard.TargetProperty="Width"
7 To="28" x:Name="activeWidth"/>
8 <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2" Storyboard.TargetProperty="Height"
9 To="28" x:Name="activeHeight"/>
10 <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2" Storyboard.TargetProperty="Opacity"
11 To="1.0"/>
12 </Storyboard>
13 <Storyboard x:Name="Deactivate">
14 <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2" Storyboard.TargetProperty="Width"
15 To="24" x:Name="deactiveWidth"/>
16 <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2" Storyboard.TargetProperty="Height"
17 To="24" x:Name="deactiveHeight"/>
18 <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2" Storyboard.TargetProperty="Opacity"
19 To="0.75"/>
20 </Storyboard>
21 </Button.Resources>
22 <Button.Content>
23 <Image x:Name="img"></Image>
24 </Button.Content>
25 </Button>
可以看到ToolbarButton继承自Button类,界面上除了动画资源外就只有一个Image控件用于显示图标。xaml.cs代码就不贴了主要就是鼠标的Leave和Enter事件,用定义好的动画实现图标的放大和还原。
下面看看Toolbar的xaml代码。
<UserControl x:Class="XCenter.Framework.Client.ToolBar"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
4 <StackPanel Orientation="Horizontal" Background="Transparent"
5 x:Name="buttonPanel" VerticalAlignment="Center">
6 </StackPanel>
7 </UserControl>
1 Toolbar的代码更简单了,就是一个StackPanel作为ToolbarButton的容器。主要逻辑其实就是取得一个唯一的Id用于判断ToolBar是否已被加载。
下面我们看看使用方法。关于Toolbar的加载我定义了一个接口,
/// <summary>
2 /// 界面支持ToolBar接口
3 /// </summary>
4 public interface IToobarable
5 {
6 /// <summary>
7 /// 获得ToolBar容器Grid
8 /// </summary>
9 /// <returns></returns>
10 ToolBar GetToolBar();
11
12 /// <summary>
13 /// 获得ToolBar唯一标实
14 /// </summary>
15 /// <returns></returns>
16 string GetToolBarId();
17 }
要添加Toolbar功能的页面(UserControl)要实现这个接口。由于我的主界面位于XCenter.UI的Page类,而Page类类似一个Frame,当单击左边的Tree菜单时在TabControl中通过反射加载指定模块的主界面实例。加载实例时可以调用IToolbarable中的方法:
tabItem.Content = uc;
//添加工具栏
if (uc is IToobarable)
{
AddToolBar(uc);
}
private void AddToolBar(UserControl uc)
2 {
3 if (uc == null)
4 return;
5 IToobarable toolbar = (IToobarable)uc;
6 if (!WindowHelper.Session.ContainsKey(SessionConst.SK_ToobarId)
7 || WindowHelper.Session[SessionConst.SK_ToobarId] == null)
8 {
9 //当前环境没有工具栏,直接添加
10 toolbarContainer.Children.Add(toolbar.GetToolBar());
11 WindowHelper.Session.Add(SessionConst.SK_ToobarId, toolbar.GetToolBarId());
12 }
13 else
14 {
15 //当前环境有工具栏,但不是此界面的工具栏
16 string toolbarid = WindowHelper.Session[SessionConst.SK_ToobarId].ToString();
17 if (!toolbarid.Equals(toolbar.GetToolBarId()))
18 {
19 toolbarContainer.Children.Clear();
20 toolbarContainer.Children.Add(toolbar.GetToolBar());
21 WindowHelper.Session[SessionConst.SK_ToobarId] = toolbar.GetToolBarId();
22 }
23 }
24 }
可以看到此处的ToobarId用于判断工具栏是否已经被加载。
至于Toolbar中的Button的是否可用,就是简单循环Toobar中的ToolbarButton并设置Enable。这在Toobar中的Button不多的情况下没有问题。多了这个办法就不行了。
(文档下载地址:http://www.ctdisk.com/file/1758321)