將次要磚釘選到開始Pin secondary tiles to Start

本主題將逐步引導您建立 Windows 應用程式的次要磚,並將其釘選至 [開始] 功能表。This topic walks you through the steps to create a secondary tile for your Windows app and pin it to the Start menu.

次要磚螢幕擷取畫面

若要深入了解次要磚,請參閱次要磚概觀To learn more about secondary tiles, please see the Secondary tiles overview.

新增命名空間Add namespace

Windows.UI.StartScreen 命名空間包含 SecondaryTile 類別。The Windows.UI.StartScreen namespace includes the SecondaryTile class.

using Windows.UI.StartScreen;

初始化次要磚Initialize the secondary tile

次要磚包含一些主要元件...Secondary tiles are composed of a few key components...

  • TileId:唯一識別碼,可讓您能在其他次要磚之間識別出該磚。TileId: A unique identifier that lets you identify the tile among your other secondary tiles.
  • DisplayName:您要在磚上顯示的名稱。DisplayName: The name you want shown on the tile.
  • Arguments:當使用者按一下您的磚時,您要傳回到應用程式的引數。Arguments: The arguments you want passed back to your app when the user clicks your tile.
  • Square150x150Logo:所需的標誌,顯示在中型磚上 (如果未提供小型標誌,可調整成小型磚)。Square150x150Logo: The required logo, displayed on the medium size tile (and resized to small size tile if no small logo provided).

必須為所有上述的屬性提供初始化的值,否則會發生例外狀況。You MUST provide initialized values for all of the above properties, or else you will get an exception.

有各種建構函式可供您使用,但使用採用 tileId、displayName、arguments、square150x150Logo 及 desiredSize 的建構函式可幫助您確認設定所有的必要屬性。There are a variety of constructors you can use, but using the constructor that takes in the tileId, displayName, arguments, square150x150Logo, and desiredSize helps ensure you set all of the required properties.

// Construct a unique tile ID, which you will need to use later for updating the tile
string tileId = "City" + zipCode;

// Use a display name you like
string displayName = cityName;

// Provide all the required info in arguments so that when user
// clicks your tile, you can navigate them to the correct content
string arguments = "action=viewCity&zipCode=" + zipCode;

// Initialize the tile with required arguments
SecondaryTile tile = new SecondaryTile(
    tileId,
    displayName,
    arguments,
    new Uri("ms-appx:///Assets/CityTiles/Square150x150Logo.png"),
    TileSize.Default);

選用:新增大型磚的支援Optional: Add support for larger tile sizes

如果您會在次要磚上顯示豐富的磚通知,您可能會想要允許使用者將其磚調寬或調大,讓他們可以看到更多的內容。If you're going to be displaying rich tile notifications on your secondary tile, you'll likely want to allow the user to resize their tile to be wide or large, so that they can see even more of your content.

若要允許磚大小更寬、更大,您必須提供 Wide310x150LogoSquare310x310LogoTo enable wide and large tile sizes, you need to provide the Wide310x150Logo and Square310x310Logo. 此外若有可能,您應為小型磚提供 Square71x71Logo (否則我們將針對小型磚縮小您所需的 Square150x150Logo)。Also, if possible, you should provide the Square71x71Logo for the small tile size (otherwise we will downsize your required Square150x150Logo for the small tile).

您也可以提供唯一的 Square44x44Logo (當出現通知時會選擇性地顯示在右下角)。You can also provide a unique Square44x44Logo, which is optionally displayed on the bottom right corner when a notification is present. 如果您未提供,則將改用您主要磚中的 Square44x44LogoIf you don't provide one, the Square44x44Logo from your primary tile will be used instead.

// Enable wide and large tile sizes
tile.VisualElements.Wide310x150Logo = new Uri("ms-appx:///Assets/CityTiles/Wide310x150Logo.png");
tile.VisualElements.Square310x310Logo = new Uri("ms-appx:///Assets/CityTiles/Square310x310Logo.png");

// Add a small size logo for better looking small tile
tile.VisualElements.Square71x71Logo = new Uri("ms-appx:///Assets/CityTiles/Square71x71Logo.png");

// Add a unique corner logo for the secondary tile
tile.VisualElements.Square44x44Logo = new Uri("ms-appx:///Assets/CityTiles/Square44x44Logo.png");

選用:允許顯示顯示名稱Optional: Enable showing the display name

顯示名稱預設為「不」顯示。By default the display name will NOT be shown. 若要在中/寬/大型磚上顯示顯示名稱,請新增下列程式碼。To show the display name on medium/wide/large, add the following code.

// Show the display name on all sizes
tile.VisualElements.ShowNameOnSquare150x150Logo = true;
tile.VisualElements.ShowNameOnWide310x150Logo = true;
tile.VisualElements.ShowNameOnSquare310x310Logo = true;

選用:3D 次要磚Optional: 3D secondary tiles

加入 3D 資產,可以美化 Windows Mixed Reality 的次要磚。You can enhance your secondary tile for Windows Mixed Reality by adding 3D assets. 在混合實境環境中使用您的應用程式時,使用者可以直接在其 Windows Mixed Reality 首頁放置 3D 磚,而不是放在 [開始] 功能表中。Users can place 3D tiles directly in their Windows Mixed Reality home instead of the Start menu when using your app in a Mixed Reality environment. 例如,您可以建立直接連結至 360 度相片檢視器應用程式的 360 度全景相片,或讓使用者從家具型錄放置椅子的 3D 模型,在選取時會開啟關於該物件的定價與色彩選項等詳細資料。For example, you can create 360° photospheres that link directly into a 360° photo viewer app, or let users place a 3D model of a chair from a furniture catalog that opens a details page about the pricing and color options for that object when selected. 若要開始,請參考混合實境開發人員文件To get started, refer to the Mixed Reality developer documentation.

釘選次要磚Pin the secondary tile

最後,要求釘選該磚。Finally, request to pin the tile. 請注意,這此必須從 UI 執行緒呼叫。Note that this must be called from a UI thread. 在桌面上將會出現一個對話方塊,要求使用者確認是否要釘選磚。On Desktop, a dialog will appear asking the user to confirm whether they would like to pin the tile.

重要

如果您是使用傳統型橋接器的桌面應用程式,您必須先執行額外的步驟,如釘選自桌面應用程式中所述If you are a desktop application using the Desktop Bridge, you must first perform an extra step as described in Pin from desktop apps

// Pin the tile
bool isPinned = await tile.RequestCreateAsync();

// TODO: Update UI to reflect whether user can now either unpin or pin

檢查次要磚是否存在Check if a secondary tile exists

如果您的使用者造訪您應用程式中他們已釘選到 [開始] 畫面的頁面,您會想要改為顯示 [取消釘選] 按鈕。If your user visits a page in your app that they have already pinned to Start, you will want to instead display an "Unpin" button.

因此在選擇要顯示哪一個按鈕時,您必須先檢查次要磚目前是否已釘選。Therefore, when choosing what button to display, you need to first check whether the secondary tile is currently pinned.

// Check if the secondary tile is pinned
bool isPinned = SecondaryTile.Exists(tileId);

// TODO: Update UI to reflect whether user can either unpin or pin

取消釘選次要磚Unpinning a secondary tile

如果次要磚目前已釘選,且使用者點擊取消釘選按鈕,您會想要取消釘選 (刪除) 次要磚。If the tile is currently pinned, and the user clicks your unpin button, you'll want to unpin (delete) the tile.

// Initialize a secondary tile with the same tile ID you want removed
SecondaryTile toBeDeleted = new SecondaryTile(tileId);

// And then unpin the tile
await toBeDeleted.RequestDeleteAsync();

更新次要磚Updating a secondary tile

如果您必須更新標誌、顯示名稱,或次要磚上的任何其他項目,則可使用 RequestUpdateAsyncIf you need to update the logos, display name, or anything else on the secondary tile, you can use RequestUpdateAsync.

// Initialize a secondary tile with the same tile ID you want to update
SecondaryTile tile = new SecondaryTile(tileId);

// Assign ALL properties, including ones you aren't changing

// And then update it
await tile.UpdateAsync();

列舉所有已釘選的次要磚Enumerating all pinned secondary tiles

如果您必須找出所有已由您使用者釘選的磚,您可以改為使用 SecondaryTile.FindAllAsync(),而非使用 SecondaryTile.ExistsIf you need to discover all the tiles your user has pinned, instead of using SecondaryTile.Exists, you can alternatively use SecondaryTile.FindAllAsync().

// Get all secondary tiles
var tiles = await SecondaryTile.FindAllAsync();

傳送磚通知Send a tile notification

若要了解如何透過磚通知在您的磚上顯示豐富內容,請參閱傳送本機磚通知To learn how to display rich content on your tile via tile notifications, please see Send a local tile notification.