操作功能表樣本這個樣本展示了如何建立一個操作功能表和如何替換預設的操作功能表文本。這個樣本使用Windows.UI。 彈出API,包括PopupMenu和oncontextmenu事件。這個樣本示範了這兩個任務:•建立一個操作功能表顯示為一個檔案•替換預設命令在操作功能表中顯示的文本瞭解選擇命令和設計一個操作功能表,看到指導方針和檢查表操作功能表。額外的api對於此樣本包括:•UICommand類•UICommandSeparator類async void Scenario2TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
// Create a menu and add commands specifying an id value for each instead of a delegate.
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("Copy", null, 1));
menu.Commands.Add(new UICommandSeparator());
menu.Commands.Add(new UICommand("Highlight", null, 2));
menu.Commands.Add(new UICommand("Look up on dictionary", null, 3));
// We don't want to obscure content, so pass in a rectangle representing the sender of the context menu event.
// We registered command callbacks; no need to await and handle context menu completion
var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
if (chosenCommand != null)
{
switch ((int)chosenCommand.Id)
{
case 1:
Output2Text.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";
break;
case 2:
Output2Text.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";
break;
case 3:
Output2Text.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";
break;
}
}
else
{
Output2Text.Text = "Context menu dismissed";
}
}
完整樣本:/Files/risk/windows8/操作功能表sample.rar