将应用固定到任务栏Pin your app to the taskbar

你可以通过编程方式将自己的应用固定到任务栏上,就像将应用固定到“开始”菜单一样。You can programmatically pin your own app to the taskbar, just like you can pin your app to the Start menu. 你可以检查应用当前是否已固定,以及任务栏是否允许固定。And you can check whether your app is currently pinned, and whether the taskbar allows pinning.

显示已固定应用程序的 Windows 10 任务栏屏幕截图。

重要

需要 Fall Creators Update:目标必须为 SDK 16299,并且必须运行版本 16299 或更高版本才能使用任务栏 API。Requires Fall Creators Update: You must target SDK 16299 and be running build 16299 or higher to use the taskbar APIs.

重要的 APITaskbarManager 类Important APIs: TaskbarManager class

应该何时请求用户将你的应用固定到任务栏?When should you ask the user to pin your app to the taskbar?

利用 TaskbarManager 类,你可以请求用户将你的应用固定到任务栏;用户必须批准该请求。The TaskbarManager class lets you ask the user to pin your app to the taskbar; the user must approve the request. 你尽了很大努力来构建一流的应用,现在你有机会请求用户将其固定到任务栏。You put a lot of effort into building a stellar app, and now you have the opportunity to ask the user to pin it to taskbar. 但在我们深入探讨代码之前,你在设计体验时需要牢记以下几点:But before we dive into the code, here are some things to keep in mind as you are designing your experience:

  • 务必使用明确的“固定到任务栏”行动号召在应用中制作无干扰并且可轻松消除的 UX。Do craft a non-disruptive and easily dismissible UX in your app with a clear "Pin to taskbar" call to action. 为此,请避免使用对话框和浮出控件。Avoid using dialogs and flyouts for this purpose.
  • 务必在要求用户固定应用前明确解释你的应用值。Do clearly explain the value of your app before asking the user to pin it.
  • 如果磁贴已经固定或设备不支持磁贴,不要请求用户固定应用。Don't ask a user to pin your app if the tile is already pinned or the device doesn’t support it. (本文说明如何确定是否支持固定。)(This article explains how to determine whether pinning is supported.)
  • 不要反复要求用户固定你的应用(用户可能会生气)。Don't repeatedly ask the user to pin your app (they will probably get annoyed).
  • 没有显式用户交互或当你的应用最小化/未打开时不要调用固定 API。Don't call the pin API without explicit user interaction or when your app is minimized/not open.

1. 检查是否存在所需的 API1. Check whether the required APIs exist

如果你的应用支持较旧的 Windows 10 版本,则需要检查 TaskbarManager 类是否可用。If your app supports older versions of Windows 10, you need to check whether the TaskbarManager class is available. 你可以使用 ApiInformation.IsTypePresent 方法执行此检查。You can use the ApiInformation.IsTypePresent method to perform this check. 如果 TaskbarManager 类不可用,请避免执行任何 API 调用。If the TaskbarManager class isn't available, avoid executing any calls to the APIs.

if (ApiInformation.IsTypePresent("Windows.UI.Shell.TaskbarManager"))
{
    // Taskbar APIs exist!
}

else
{
    // Older version of Windows, no taskbar APIs
}

2. 检查任务栏是否存在并允许固定2. Check whether taskbar is present and allows pinning

Windows 应用可在各种设备上运行;并不是所有的都支持任务栏。Windows apps can run on a wide variety of devices; not all of them support the taskbar. 现在,仅桌面设备支持任务栏。Right now, only Desktop devices support the taskbar.

即使任务栏可用,用户计算机上的组策略也可能会禁用任务栏固定。Even if the taskbar is available, a group policy on the user's machine might disable taskbar pinning. 因此,尝试固定应用之前,你需要检查是否支持固定到任务栏。So, before you attempt to pin your app, you need to check whether pinning to the taskbar is supported. 如果任务栏存在并且允许固定,则 TaskbarManager.IsPinningAllowed 属性 会返回 True。The TaskbarManager.IsPinningAllowed property returns true if the taskbar is present and allows pinning.

// Check if taskbar allows pinning (Group Policy can disable it, or some device families don't have taskbar)
bool isPinningAllowed = TaskbarManager.GetDefault().IsPinningAllowed;

备注

如果你不希望将应用固定到任务栏,只是想了解任务栏是否可用,请使用 TaskbarManager.IsSupported 属性If you don't want to pin your app to the taskbar and just want to find out whether the taskbar is available, use the TaskbarManager.IsSupported property.

3. 检查应用当前是否已固定到任务栏3. Check whether your app is currently pinned to the taskbar

显然,如果应用已固定到任务栏,则请求用户允许你将应用固定到任务栏毫无意义。Obviously, there's no point in asking the user to let you pin the app to the taskbar if it's already pinned there. 你可以在请求用户之前使用 TaskbarManager.IsCurrentAppPinnedAsync 方法检查应用是否已固定。You can use the TaskbarManager.IsCurrentAppPinnedAsync method to check whether the app is already pinned before asking the user.

// Check whether your app is currently pinned
bool isPinned = await TaskbarManager.GetDefault().IsCurrentAppPinnedAsync();

if (isPinned)
{
    // The app is already pinned--no point in asking to pin it again!
}
else 
{
    //The app is not pinned. 
}

4. 固定应用4. Pin your app

如果任务栏存在且允许固定,并且当前未固定你的应用,则你可能需要显示一条巧妙的提示,让用户知道他们可以固定你的应用。If the taskbar is present and pinning is allowed and your app currently isn't pinned, you might want to show a subtle tip to let users know that they can pin your app. 例如,你可以在用户能够单击的 UI 中的某个地方显示固定图标。For example, you might show a pin icon somewhere in your UI that the user can click.

如果用户单击你的固定建议 UI,则将调用 TaskbarManager.RequestPinCurrentAppAsync 方法If the user clicks your pin suggestion UI, you would then call the TaskbarManager.RequestPinCurrentAppAsync method. 此方法会显示一个对话框,并请求用户确认他们想要将你的应用固定到任务栏。This method displays a dialog that asks the user to confirm that they want your app pinned to the taskbar.

重要

这必须从前台 UI 线程中进行调用,否则将出现异常。This must be called from a foreground UI thread, otherwise an exception will be thrown.

// Request to be pinned to the taskbar
bool isPinned = await TaskbarManager.GetDefault().RequestPinCurrentAppAsync();

固定对话框

此方法会返回一个布尔值,指明你的应用现在是否已固定到任务栏。This method returns a boolean value that indicates whether your app is now pinned to the taskbar. 如果你的应用已固定,则此方法会立即返回 True,而不向用户显示对话框。If your app was already pinned, the method immediately returns true without showing the dialog to the user. 如果用户在对话框中单击“否”或者不允许将你的应用固定到任务栏,则此方法会返回 False。If the user clicks "no" on the dialog, or pinning your app to the taskbar isn't allowed, the method returns false. 相反,如果用户单击“是”并且固定了应用,则 API 将返回 True。Otherwise, the user clicked yes and the app was pinned, and the API will return true.

资源Resources