原文: 2、Popup 的示例Controls/FlyoutControl/PopupDemo.xaml 3、PopupMenu 的示例Controls/FlyoutControl/PopupMenuDemo.xaml OK
背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
作者:介绍背水一战 Windows 10 之 控件(弹出类) - ToolTip
- Popup
- PopupMenu
Controls/FlyoutControl/ToolTipDemo.xaml.cs
/* * ToolTip - 提示框控件(继承自 ContentControl, 请参见 /Controls/BaseControl/ContentControlDemo/) */using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;namespace Windows10.Controls.FlyoutControl{ public sealed partial class ToolTipDemo : Page { public ToolTipDemo() { this.InitializeComponent(); } private void toolTip_Opened(object sender, RoutedEventArgs e) { lblMsg.Text = "textBlock2 toolTip_Opened"; } private void toolTip_Closed(object sender, RoutedEventArgs e) { lblMsg.Text = "textBlock2 toolTip_Closed"; } }}
Controls/FlyoutControl/PopupDemo.xaml.cs
/* * Popup - 弹出框控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo.xaml) * IsOpen - 弹出框是否是打开的状态(如果要设置此属性,需要在控件加载之后) * Opened - 弹出框打开后触发的事件 * Closed - 弹出框关闭后触发的事件 */using Windows.UI;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Media.Animation;namespace Windows10.Controls.FlyoutControl{ public sealed partial class PopupDemo : Page { // 仿 toast 的 Popup private Popup _popupToast = new Popup(); public PopupDemo() { this.InitializeComponent(); popup.Opened += delegate { lblMsg.Text = "popup.Opened"; }; popup.Closed += delegate { lblMsg.Text = "popup.Closed"; }; } private void btnOpenPopup_Click(object sender, RoutedEventArgs e) { if (!popup.IsOpen) popup.IsOpen = true; } private void btnClosePopup_Click(object sender, RoutedEventArgs e) { if (popup.IsOpen) popup.IsOpen = false; } private void btnOpenPopupToast_Click(object sender, RoutedEventArgs e) { if (!_popupToast.IsOpen) { // 设置 Popup 中的内容 Border border = new Border(); border.BorderBrush = new SolidColorBrush(Colors.Red); border.BorderThickness = new Thickness(1); border.Background = new SolidColorBrush(Colors.Blue); border.Width = 600; border.Height = 100; border.Child = new TextBlock() { Text = "我是 Popup", HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; // 设置 Popup 的相关属性 _popupToast.IsLightDismissEnabled = true; _popupToast.Child = border; _popupToast.VerticalOffset = 100d; // 设置 Popup 的显示位置(Popup 的默认显示位置在窗口 0,0 点) _popupToast.ChildTransitions = new TransitionCollection() { new PaneThemeTransition() { Edge = EdgeTransitionLocation.Left } }; _popupToast.IsOpen = true; } } }}
鼠标右键我或触摸 press-and-hold 我,以弹出 PopupMenu
Controls/FlyoutControl/PopupMenuDemo.xaml.cs
/* * PopupMenu - 上下文菜单(未继承任何类) * Commands - 上下文菜单中的命令集合,返回 IList类型的数据 * IAsyncOperation ShowAsync(Point invocationPoint) - 在指定的位置(PopupMenu 的默认显示位置在窗口 0,0 点)上显示上下文菜单,并返回用户激发的命令 * IAsyncOperation ShowForSelectionAsync(Rect selection, Placement preferredPlacement) - 在指定的矩形区域的指定方位显示上下文菜单,并返回用户激发的命令 * * IUICommand - 命令 * Label - 显示的文字 * Id - 参数 * * UICommandSeparator - 分隔符 */using System;using Windows.UI.Popups;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Input;using Windows10.Common;namespace Windows10.Controls.FlyoutControl{ public sealed partial class PopupMenuDemo : Page { public PopupMenuDemo() { this.InitializeComponent(); lblDemo.RightTapped += lblDemo_RightTapped; } private async void lblDemo_RightTapped(object sender, RightTappedRoutedEventArgs e) { PopupMenu menu = new PopupMenu(); menu.Commands.Add ( new UICommand ( "item1", (command) => { lblMsg.Text = string.Format("command label:{0}, id:{1}", command.Label, command.Id); }, "param1" ) ); menu.Commands.Add ( new UICommand ( "item2", (command) => { lblMsg.Text = string.Format("command label:{0}, id:{1}", command.Label, command.Id); }, "param2" ) ); // 分隔符 menu.Commands.Add(new UICommandSeparator()); menu.Commands.Add ( new UICommand ( "item3", (command) => { lblMsg.Text = string.Format("command label:{0}, id:{1}", command.Label, command.Id); }, "param3" ) ); // 在指定的位置显示上下文菜单,并返回用户激发的命令(测试的时候这里有时会发生异常,不知道什么原因,所以还是尽量用 MenuFlyout 吧) IUICommand chosenCommand = await menu.ShowForSelectionAsync(Helper.GetElementRect((FrameworkElement)sender), Placement.Below); if (chosenCommand == null) // 用户没有在上下文菜单中激发任何命令 { lblMsg.Text = "用户没有选择任何命令"; } else { lblMsg.Text += Environment.NewLine; lblMsg.Text += string.Format("result label:{0}, id:{1}", chosenCommand.Label, chosenCommand.Id); } } }}
posted on 2017-09-21 10:38 阅读( ...) 评论( ...)