Windows 7 任務列的 Jump List(跳轉清單)讓使用者能直接從圖示右鍵選單啟動常用功能、開啟近期檔案。WPF 4 內建 System.Windows.Shell 命名空間,支援純 XAML 宣告式與 C# 程式碼雙重方式建立 Jump List,大幅簡化開發。
在 App.xaml 直接定義:
<Application x:Class="Win7TaskbarDemo.App"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources/>
<JumpList.JumpList>
<JumpList ShowFrequentCategory="True"
ShowRecentCategory="True">
<!-- 自訂類別:Microsoft Tools -->
<JumpTask ApplicationPath="notepad.exe"
Title="Notepad"
Description="Start Notepad"
CustomCategory="Microsoft Tools"
IconResourcePath="notepad.exe"
IconResourceIndex="0" />
<JumpTask ApplicationPath="mspaint.exe"
Title="Paint"
Description="Start Paint"
CustomCategory="Microsoft Tools"
IconResourcePath="mspaint.exe"
IconResourceIndex="0" />
<!-- 網站連結 -->
<JumpTask ApplicationPath="https://gnielee.cnblogs.com/"
Title="Gnie's Blog"
Description="Go to {GnieTech}"
CustomCategory="Blog Link"
IconResourcePath="C:\Program Files\Internet Explorer\iexplore.exe" />
</JumpList>
</JumpList.JumpList>
</Application>
屬性說明:
• ShowFrequentCategory:顯示「常用」類別(系統自動統計)
• ShowRecentCategory:顯示「最近」類別(系統自動統計)
• CustomCategory:自訂分組名稱
• IconResourceIndex:圖示資源索引(單一圖示檔為 0)
主視窗 XAML 放置兩個按鈕:
<Grid>
<Button Content="Add Calculator" Click="AddBtn_Click"
Margin="60,29,0,0" Width="93" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="Clear All Tasks" Click="ClearBtn_Click"
Margin="0,29,59,0" Width="89" Height="23" HorizontalAlignment="Right" VerticalAlignment="Top"/>
</Grid>
後置程式碼:
using System;
using System.IO;
using System.Windows;
using System.Windows.Shell; // 關鍵命名空間
namespace Win7TaskbarDemo
{
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }
private void AddBtn_Click(object sender, RoutedEventArgs e)
{
var jumpTask = new JumpTask
{
ApplicationPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.System), "calc.exe"),
IconResourcePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.System), "calc.exe"),
Title = "Calculator",
Description = "Start Calculator",
CustomCategory = "New Microsoft Tools"
};
JumpList jumpList = JumpList.GetJumpList(App.Current);
jumpList.JumpItems.Add(jumpTask);
jumpList.Apply(); // 必須呼叫 Apply() 才會生效
}
private void ClearBtn_Click(object sender, RoutedEventArgs e)
{
JumpList jumpList = JumpList.GetJumpList(App.Current);
jumpList.JumpItems.Clear();
jumpList.Apply();
}
}
}
| 類別 | 命名空間 | 用途 |
|---|---|---|
| JumpList | System.Windows.Shell | 管理跳轉清單,提供 GetJumpList、Apply、JumpItems |
| JumpTask | System.Windows.Shell | 代表單一啟動項目(應用程式、連結、文件) |
| JumpPath | System.Windows.Shell | 代表檔案路徑項目(自動關聯預設開啟程式) |
jumpList.JumpItems.Add(new Separator())(需參考 System.Windows.Controls)。JumpTask 歸屬 UserTasks 類別,優先顯示於清單頂端;ShowRecentCategory/ShowFrequentCategory 由 Shell 自動維護。IconResourcePath 支援 .exe、.dll、.ico,索引對應資源內部順序。完整 VS2010 專案:Win7TaskbarDemo.VS2010.zip
WPF 4 讓 Jump List 開發從「呼叫 Win32 API、手寫 COM 介面」進化為「幾行 XAML 或 C# 即完成」。靜態選單用 XAML 宣告,動態選單用 C# 操作 JumpList.GetJumpList(App.Current),兩者互補,輕鬆打造專業級 Windows 7 體驗。