您的位置:首页 > 其它

Windows 7 任务栏开发 之 跳转列表(Jump Lists)

2010-03-16 11:17 323 查看
       本篇我们开始介绍任务栏的另一个亮点:跳转列表(Jump Lists,下文简称JL)。JL 可以使用户方便快捷的找到想要浏览的文件(文档、图片、音频或视频等)以及应用程序的链接或快捷方式。以IE 浏览器为例看看JL 都具备哪些功能:



 

       · 在红色区域“Taskbar Tasks” 放置了应用程序的一些默认任务:“打开IE 浏览器”、“从任务栏取消固定”、“关闭程序”。无论是否对JL 做过开发“Taskbar Tasks” 列表都会出现在所有的应用程序中,例如之前的实例程序(如下图)。



       · “User Tasks” 包含了应用程序本身提供的一些功能,通过这些链接可以直接对应用程序进行操作。例如,打开一个新IE 标签。

       · “Known Category” 这个列表是Windows 7 默认类别,其中包含三种模式:“Recent”(近期浏览)、“Frequent”(经常浏览)、“Neither”。下图即为Frequent 模式,它的功能是将经常浏览的网页内容记录下来以便日后再次浏览,随着时间的流逝该列表中的网页链接会随之变化或消失。除了“Known Category” 列表外同样也以创建“Custom Category”(下文将会慢慢讲到)。

       · “Pinned Category” 正如上面所讲“Frequent Category” 列表中的网页会经常变化,通过右键将网页“钉”在列表中可使其永久保存(如下图)。



 

创建User Tasks 列表

      现在是不是也想为自己的程序添加一个JL,下面先来介绍如何创建User Tasks 列表。1. 通过JumpList 类创建一个JL 实例。2. 使用JumpListLink(string pathValue, string titleValue) 方法(pathValue:应用程序路径,titleValue:链接名称),可以将“记事本”、“画板”这样的Windows 应用程序,以及“网站地址”创建为User Tasks 链接。3. 再使用AddUserTasks(params IJumpListTask[] tasks) 方法将这些链接添加到JL 中。如下代码所示:

private JumpList jumpList;
private string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
private void addApps_Click(object sender, RoutedEventArgs e)
{
IJumpListTask notepadTask = new JumpListLink(Path.Combine(systemPath, "notepad.exe"), "Notepad")
{
IconReference = new IconReference(Path.Combine(systemPath, "notepad.exe"), 0)
};

IJumpListTask paintTask = new JumpListLink(Path.Combine(systemPath, "mspaint.exe"), "Paint")
{
IconReference = new IconReference(Path.Combine(systemPath, "mspaint.exe"), 0)
};

IJumpListTask jlSeparator = new JumpListSeparator();

IJumpListTask linkTask = new JumpListLink("http://gnielee.cnblogs.com", "Gnie's Blog")
{
IconReference = new IconReference("C:\\Program Files\\Internet Explorer\\iexplore.exe", 0)
};

jumpList.AddUserTasks(notepadTask, paintTask, jlSeparator, linkTask);
jumpList.Refresh();
}


       在上面程序中,通过IJumpListTask 接口创建了“程序链接”(JumpListLink,其中IconReference 为链接图标)和“分割线”(JumpListSeparator);使用AddUserTasks 方法时注意每个链接的位置关系;最后必须使用Refresh 方法对JL 进行刷新才能显示出最新的JL 内容(如下效果图)。



 

创建Known Category 列表

       在使用Known Category 功能前,需要先为程序注册文件类型,随后可通过KnownCategoryToDisplay 属性将Known Category 预设为“Recent”、“Frequent”、“Neither” 中的任意一种类型,当测试程序打开某个的文件时,相应的文件链接就会显示在Known Category 列表中。如下代码所示:

if (!Utilities.IsApplicationRegistered(TaskbarManager.Instance.ApplicationId))
{
Utilities.RegisterFileAssociations(TaskbarManager.Instance.ApplicationId, false,
TaskbarManager.Instance.ApplicationId, Assembly.GetExecutingAssembly().Location,
".jpg", ".png", ".gif", ".JPG", ".PNG", ".GIF");
}
jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
CommonOpenFileDialog cfd = new CommonOpenFileDialog();
cfd.ShowDialog();
jumpList.Refresh();


打开demo.png 文件后的JL 效果:



 

       JumpListKnownCategoryType 枚举中定义了Known Category 的三种类型:“Neither”、“Recent”、“Frequent”,还可以通过KnownCategoryOrdinalPosition 属性可以修改Known Category 在JL 中的位置:

switch (this.knownCategory.SelectionBoxItem.ToString())
{
case "None":
jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Neither;
break;
case "Recent":
jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
break;
case "Frequent":
jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Frequent;
break;
}
jumpList.KnownCategoryOrdinalPosition = Convert.ToInt32(this.categoryPostion.SelectionBoxItem.ToString());
jumpList.Refresh();


将Recent 修改为Frequent 后的效果:



 

创建Custom Category 列表

       如同上文创建JumpList 的方式,1. 通过JumpListCustomCategory 类创建“自定义分类”列表实例。2. 由JumpListCustomCategory(string categoryName) 方法为列表命名。3. 使用AddJumpListItems 方法将链接加入到分类中。如下代码所示:

private JumpListCustomCategory customCategory;
private void addCus_Click(object sender, RoutedEventArgs e)
{
if (this.categoryName.Text.Length > 0)
{
customCategory = new JumpListCustomCategory(this.categoryName.Text);
jumpList.AddCustomCategories(customCategory);

JumpListLink jlItem = new JumpListLink(Assembly.GetExecutingAssembly().Location, this.categoryName.Text + ".png")
{
IconReference = new IconReference(Assembly.GetEntryAssembly().Location, 0)
};
customCategory.AddJumpListItems(jlItem);
jumpList.Refresh();
}
}



      

 
                                                                   KnownCategoryOrdinalPosition效果

 

       在上面代码中IconReference 取自应用程序本身的Icon 属性,前提是需要在应用程序属性中为其设置图标资源(如下图所示)。至此,在Windows 7 中对任务栏的相关开发已全部完成,希望本系列对大家有所帮助。



 

系列文章索引

· Windows 7 任务栏开发 之 覆盖图标(Overlay Icon)

· Windows 7 任务栏开发 之 进度条(Progress Bar)

· Windows 7 任务栏开发 之 缩略图预览(Thumbnail)

· Windows 7 任务栏开发 之 缩略图工具栏(Thumbnail Toolbar)

 

相关参考资料

1. Jump into the Windows 7 Taskbar Jump Lists

http://channel9.msdn.com/posts/yochay/Jump-into-the-Windows-7-Taskbar-Jump-Lists/

2. Windows 7 Jump Lists

http://blogs.msdn.com/coding4fun/archive/2009/12/09/9933039.aspx

3. IconReference – Using own icons

http://dennisdel.com/?p=38

 

完整源代码下载

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐