本篇我們開始介紹工作列的另一個亮點:捷徑清單(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
完整原始碼下載