將應用程式釘選到工作列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. 檢查所需的 API 是否存在1. 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 methodIf 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