JumpList 类

定义

表示作为菜单显示在 Windows 7 任务栏按钮上的项和任务的列表。

public ref class JumpList sealed : System::ComponentModel::ISupportInitialize
[System.Windows.Markup.ContentProperty("JumpItems")]
public sealed class JumpList : System.ComponentModel.ISupportInitialize
[System.Windows.Markup.ContentProperty("JumpItems")]
[System.Security.SecurityCritical]
public sealed class JumpList : System.ComponentModel.ISupportInitialize
[<System.Windows.Markup.ContentProperty("JumpItems")>]
type JumpList = class
    interface ISupportInitialize
[<System.Windows.Markup.ContentProperty("JumpItems")>]
[<System.Security.SecurityCritical>]
type JumpList = class
    interface ISupportInitialize
Public NotInheritable Class JumpList
Implements ISupportInitialize
继承
JumpList
属性
实现

示例

以下示例演示具有跳转列表的应用程序。 该应用程序有三个按钮,可用于向当前跳转列表添加任务、清除跳转列表的内容,并将新的跳转列表应用于应用程序。

以下示例演示如何在标记中声明 JumpList 。 包含 JumpList 两个 JumpTask 链接和一个 JumpPathJumpPath如果应用程序未注册以处理.txt文件扩展名,则向 shell 应用将失败。

<Application x:Class="JumpListSample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <JumpList.JumpList>
        <JumpList ShowRecentCategory="True"
                  ShowFrequentCategory="True"
                  JumpItemsRejected="JumpList_JumpItemsRejected"
                  JumpItemsRemovedByUser="JumpList_JumpItemsRemovedByUser">
            <JumpTask Title="Notepad" 
                      Description="Open Notepad." 
                      ApplicationPath="C:\Windows\notepad.exe"
                      IconResourcePath="C:\Windows\notepad.exe"/>
            <JumpTask Title="Read Me" 
                      Description="Open readme.txt in Notepad." 
                      ApplicationPath="C:\Windows\notepad.exe"
                      IconResourcePath="C:\Windows\System32\imageres.dll"
                      IconResourceIndex="14"
                      WorkingDirectory="C:\Users\Public\Documents"
                      Arguments="readme.txt"/>
            <JumpPath Path="C:\Users\Public\Documents\readme.txt" />
        </JumpList>
    </JumpList.JumpList>
</Application>

以下示例显示了 的代码 App.xaml隐藏页。 此代码处理 JumpItemsRejectedJumpItemsRemovedByUser 事件。

using System.Text;
using System.Windows;
using System.Windows.Shell;

namespace JumpListSample
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void JumpList_JumpItemsRejected(object sender, System.Windows.Shell.JumpItemsRejectedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} Jump Items Rejected:\n", e.RejectionReasons.Count);
            for (int i = 0; i < e.RejectionReasons.Count; ++i)
            {
                if (e.RejectedItems[i].GetType() == typeof(JumpPath))
                    sb.AppendFormat("Reason: {0}\tItem: {1}\n", e.RejectionReasons[i], ((JumpPath)e.RejectedItems[i]).Path);
                else
                    sb.AppendFormat("Reason: {0}\tItem: {1}\n", e.RejectionReasons[i], ((JumpTask)e.RejectedItems[i]).ApplicationPath);
            }

            MessageBox.Show(sb.ToString());
        }

        private void JumpList_JumpItemsRemovedByUser(object sender, System.Windows.Shell.JumpItemsRemovedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} Jump Items Removed by the user:\n", e.RemovedItems.Count);
            for (int i = 0; i < e.RemovedItems.Count; ++i)
            {
                sb.AppendFormat("{0}\n", e.RemovedItems[i]);
            }

            MessageBox.Show(sb.ToString());
        }
    }
}

以下示例显示了用于创建应用程序用户界面的标记。

<Window x:Class="JumpListSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Jump List Sample" Height="240" Width="500">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="200" />
            <Setter Property="Margin" Value="5" />
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Content="Add Task to JumpList" Click="AddTask" />
            <Button Content="Clear Jump List" Click="ClearJumpList"/>
            <Button Content="Set New Jump List" Click="SetNewJumpList" />
        </StackPanel>
    </Grid>
</Window>

以下示例显示了 的代码 MainWindow.xaml隐藏页。 此代码演示如何在过程代码中修改、清除和创建 JumpList 。 对于新的跳转列表,调用静态 SetJumpList 方法以将 与当前应用程序相关联 JumpListJumpList 并将 应用于 Windows shell。

using System;
using System.IO;
using System.Windows;
using System.Windows.Shell;

namespace JumpListSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void AddTask(object sender, RoutedEventArgs e)
        {
            // Configure a new JumpTask.
            JumpTask jumpTask1 = new JumpTask();
            // Get the path to Calculator and set the JumpTask properties.
            jumpTask1.ApplicationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
            jumpTask1.IconResourcePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
            jumpTask1.Title = "Calculator";
            jumpTask1.Description = "Open Calculator.";
            jumpTask1.CustomCategory = "User Added Tasks";
            // Get the JumpList from the application and update it.
            JumpList jumpList1 = JumpList.GetJumpList(App.Current);
            jumpList1.JumpItems.Add(jumpTask1);
            JumpList.AddToRecentCategory(jumpTask1);
            jumpList1.Apply();
        }
        private void ClearJumpList(object sender, RoutedEventArgs e)
        {
            JumpList jumpList1 = JumpList.GetJumpList(App.Current);
            jumpList1.JumpItems.Clear();
            jumpList1.Apply();
        }
        private void SetNewJumpList(object sender, RoutedEventArgs e)
        {
            //Configure a new JumpTask
            JumpTask jumpTask1 = new JumpTask();
            // Get the path to WordPad and set the JumpTask properties.
            jumpTask1.ApplicationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "write.exe");
            jumpTask1.IconResourcePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "write.exe");
            jumpTask1.Title = "WordPad";
            jumpTask1.Description = "Open WordPad.";
            jumpTask1.CustomCategory = "Jump List 2";
            // Create and set the new JumpList.
            JumpList jumpList2 = new JumpList();
            jumpList2.JumpItems.Add(jumpTask1);
            JumpList.SetJumpList(App.Current, jumpList2);
        }
    }
}

注解

Windows 7 任务栏提供增强的功能,用于使用跳转列表直接从任务栏按钮启动程序。 跳转列表也用于 Windows 7“开始”菜单。 可通过右键单击任务栏按钮或单击“开始”菜单中某个程序旁边的箭头来访问跳转列表。 有关跳转列表的详细信息,请参阅 Windows 用户体验交互指南的任务 部分。

JumpList 为 Windows 7 任务栏中的跳转列表功能提供托管包装器,并管理传递到 Windows shell 的数据。 类公开 JumpList 的功能在早于 Windows 7 的 Windows 版本中不可用。 使用 JumpList 类的应用程序将在其他版本的 Windows 中运行,但跳转列表将不可用。 有关 Windows shell 和本机跳转列表 API 的详细信息,请参阅 任务栏扩展

下图显示了用于Windows 媒体播放器的跳转列表,其中包含“任务”和“常用”类别中的项。

Windows 媒体播放器跳转列表
Windows Media Player 跳跃菜单

配置跳转列表

跳转列表可以包含两种类型的项:和 JumpTaskJumpPath。 是 JumpTask 程序的链接, JumpPath 是指向文件的链接。 可以通过创建没有 Title 指定 和 CustomCategory 的 来JumpTask直观地分隔跳转列表中的项。 此空 JumpTask 将在跳转列表中显示为水平线。

注意

如果 中指定的 JumpPath 文件类型未注册到应用程序,则该文件不会显示在跳转列表中。 例如,如果添加 JumpPath 指向 .txt 文件的 ,则必须注册应用程序才能处理.txt文件。 有关详细信息,请参阅 文件关联简介

跳转项放置在 中的类别中 JumpList。 默认情况下,JumpItem 显示在“任务”类别中。 或者,可以为 指定 CustomCategoryJumpItem

可以通过设置 和 ShowFrequentCategory 属性来指定是否在 中JumpList显示标准“最近”和“常用ShowRecentCategory类别。 这些类别的内容由 Windows shell 管理。 由于这些类别可能包含许多相同的数据,因此通常会在 中 JumpList显示一个或另一个数据,但不能同时显示两者。 如果最近通过通用文件对话框打开或用于通过文件类型关联打开应用程序,Windows 会自动管理这些项。 通过跳转列表访问项时,可以通过调用 AddToRecentCategory 方法通知 Windows shell 将项添加到“最近”类别。

将跳转列表应用于 Windows Shell

不能直接访问 shell 的跳转列表,也不能将其内容读入 JumpList 类。 相反, JumpList 类提供可以使用的跳转列表的表示形式,然后应用于 Windows shell。 通常,在首次运行应用程序时创建 JumpList 并设置一次。 但是,可以在运行时修改或替换 JumpList

设置 JumpList 属性后,必须先将 应用到 JumpList Windows shell,然后才能在任务栏跳转列表中显示其内容。 这在首次附加到应用程序时 JumpList 自动完成,无论是在 XAML 中还是在调用 SetJumpList 方法中。 如果在运行时修改 的内容 JumpList ,则必须调用 Apply 方法以将其当前内容应用于 Windows shell。

在 XAML 中设置跳转列表

JumpList不会自动附加到 Application 对象。 使用附加 JumpList 属性语法将 附加到 Application XAML 中的 对象。 类 JumpList 实现 ISupportInitialize 接口以支持 的 JumpListXAML 声明。 JumpList如果在 XAML 中声明并附加到当前 Application,则初始化 时JumpList会自动将其应用于 Windows shell。

在代码中设置和修改跳转列表

通过调用静态 SetJumpList 方法,Application将 附加到JumpList代码中的 对象。 这也适用于 JumpList Windows shell。

若要在运行时修改 , JumpList 请调用 GetJumpList 方法以获取 JumpList 当前附加到 的 Application。 修改 JumpList的属性后,必须调用 Apply 方法以将更改应用到 Windows shell。

注意

通常创建一个 JumpList 附加到 Application 并应用于 Windows shell 的 。 但是,可以创建多个 JumpList 对象。 一次只能将一 JumpList 个应用于 Windows shell,并且一次只能将一 JumpListApplication与 关联。 .NET Framework不要求这些是相同的 JumpList

注意

此类包含在类级别应用于所有成员的链接需求。 SecurityException当直接调用方没有完全信任权限时,将引发 。 有关安全要求的详细信息,请参阅 链接需求继承要求

构造函数

JumpList()

初始化 JumpList 类的新实例。

JumpList(IEnumerable<JumpItem>, Boolean, Boolean)

使用指定的参数初始化 JumpList 类的新实例。

属性

JumpItems

获取在跳转列表中显示的 JumpItem 对象的集合。

ShowFrequentCategory

获取或设置一个值,该值指示常用项是否显示在跳转列表中。

ShowRecentCategory

获取或设置一个值,该值指示最近使用的项是否显示在跳转列表中。

方法

AddToRecentCategory(JumpPath)

将指定的跳转路径添加到跳转列表的“最近”类别中

AddToRecentCategory(JumpTask)

将指定的跳转任务添加到跳转列表的“最近”类别中

AddToRecentCategory(String)

将指定的项路径添加到跳转列表的“最近”类别中

Apply()

在其当前状态下将 JumpList 发送到 Windows shell 中。

BeginInit()

用信号通知 JumpList 初始化开始。

EndInit()

用信号通知 JumpList 初始化结束。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetJumpList(Application)

返回与某个应用程序关联的 JumpList 对象。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
SetJumpList(Application, JumpList)

设置与应用程序关联的 JumpList 对象。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

事件

JumpItemsRejected

在 Windows shell 将跳转项添加到跳转列表中失败时发生。

JumpItemsRemovedByUser

在用户从跳转列表中移除以前位于该列表中的跳转项时发生。

适用于