首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WPF 4 輕鬆打造 Windows 7 Jump List:XAML 與 C# 雙軌實作

     編輯:關於.NET

Windows 7 任務列的 Jump List(跳轉清單)讓使用者能直接從圖示右鍵選單啟動常用功能、開啟近期檔案。WPF 4 內建 System.Windows.Shell 命名空間,支援純 XAML 宣告式與 C# 程式碼雙重方式建立 Jump List,大幅簡化開發。

方式一:純 XAML 宣告(最簡潔)

在 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)

方式二:C# 動態操作(彈性最高)

主視窗 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();
        }
    }
}

執行效果

  • 啟動應用 → 任務列圖示右鍵 → 出現「Microsoft Tools」群組,含 Notepad、Paint、「Blog Link」群組含部落格連結。
  • 點擊「Add Calculator」→ 重新右鍵 → 新增「New Microsoft Tools」群組含 Calculator。
  • 點擊「Clear All Tasks」→ 所有自訂項目清空,僅保留系統自動管理的常用/最近類別。

關鍵類別速查表

類別命名空間用途
JumpListSystem.Windows.Shell管理跳轉清單,提供 GetJumpList、Apply、JumpItems
JumpTaskSystem.Windows.Shell代表單一啟動項目(應用程式、連結、文件)
JumpPathSystem.Windows.Shell代表檔案路徑項目(自動關聯預設開啟程式)

進階技巧

  1. 分隔線:jumpList.JumpItems.Add(new Separator())(需參考 System.Windows.Controls)。
  2. 使用者任務 vs 最近/常用:JumpTask 歸屬 UserTasks 類別,優先顯示於清單頂端;ShowRecentCategory/ShowFrequentCategory 由 Shell 自動維護。
  3. 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 體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved