设为首页 收藏本站
查看: 708|回复: 0

[经验分享] 背水一战 Windows 10 (36)

[复制链接]
发表于 2017-6-29 06:48:42 | 显示全部楼层 |阅读模式
  [源码下载]




背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu  
作者:webabcd

介绍
背水一战 Windows 10 之 控件(弹出类)


  • ToolTip
  • Popup
  • PopupMenu
  
示例
1、ToolTip 的示例
Controls/FlyoutControl/ToolTipDemo.xaml



<Page
x:Class="Windows10.Controls.FlyoutControl.ToolTipDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.FlyoutControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="10 0 10 10">
<!--
ToolTip - 提示框控件
Content - 提示内容
Placement - 提示框的显示位置(Bottom, Right, Mouse, Left, Top)
HorizontalOffset - 水平偏移量
VerticalOffset - 垂直偏移量
IsOpen - 提示框是否是显示状态(如果要设置此属性的话,需要等到宿主加载完之后再设置)
Closed - 提示框关闭后触发的事件
Opened - 提示框打开后触发的事件
注:我这里测试 ToolTip 的最大宽度是 320
-->
<TextBlock Name="textBlock1" Text="TextBlock" Margin="5"
ToolTipService.ToolTip="ToolTip 的内容"
ToolTipService.Placement="Right" />
<TextBlock Name="textBlock2" Text="TextBlock" Margin="5">
<ToolTipService.ToolTip>
<ToolTip Content="ToolTip 的内容" Placement="Mouse"
HorizontalOffset="120" VerticalOffset="0"
Opened="toolTip_Opened" Closed="toolTip_Closed" />
</ToolTipService.ToolTip>
</TextBlock>
<TextBlock Name="lblMsg" Margin="5" />
</StackPanel>
</Grid>
</Page>
  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";
}
}
}
  
2、Popup 的示例
Controls/FlyoutControl/PopupDemo.xaml



<Page
x:Class="Windows10.Controls.FlyoutControl.PopupDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.FlyoutControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="10 0 10 10">
<!--
Popup - 弹出框控件
Child - 弹出框的内容([ContentProperty(Name = "Child")]),一个 UIElement 类型的对象
ChildTransitions - 显示弹出框时,其内容的过渡效果
IsLightDismissEnabled - 点击非 Popup 区域时否关闭 Popup
HorizontalOffset - 水平方向上的偏移量
VerticalOffset - 垂直方向上的偏移量
-->
<Popup Name="popup" Margin="5"
HorizontalOffset="200" VerticalOffset="10" IsLightDismissEnabled="{Binding IsChecked, ElementName=chkIsLightDismissEnabled}">
<Border BorderBrush="Red" BorderThickness="1" Background="Orange" Width="200" Height="200">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="我是 Popup" HorizontalAlignment="Center" />
<Button Name="btnClosePopup" Content="关闭" HorizontalAlignment="Center" Click="btnClosePopup_Click" />
</StackPanel>
</Border>
<!--为弹出框增加 PopupThemeTransition 效果-->
<Popup.ChildTransitions>
<TransitionCollection>
<PopupThemeTransition />
</TransitionCollection>
</Popup.ChildTransitions>
</Popup>
<TextBlock Name="lblMsg" Margin="5" />

<!--
显示 xaml 方式定义的 popup
-->
<StackPanel Orientation="Horizontal" Margin="5">
<Button Name="btnOpenPopup" Content="弹出 Popup" Click="btnOpenPopup_Click" />
<CheckBox Name="chkIsLightDismissEnabled" IsChecked="False" Content="IsLightDismissEnabled" Margin="10 0 0 0" />
</StackPanel>

<!--
显示 code-behind 方式定义的 popup
-->
<Button Name="btnOpenPopupToast" Content="弹出仿 toast 的 Popup" Click="btnOpenPopupToast_Click" Margin="5" />
</StackPanel>
</Grid>
</Page>
  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;
}
}
}
}
  
3、PopupMenu 的示例
Controls/FlyoutControl/PopupMenuDemo.xaml



<Page
x:Class="Windows10.Controls.FlyoutControl.PopupMenuDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.FlyoutControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="10 0 10 10">
<TextBlock Name="lblMsg" Margin="5" />
<TextBlock Name="lblDemo" Margin="5">
鼠标右键我或触摸 press-and-hold 我,以弹出 PopupMenu
</TextBlock>
</StackPanel>
</Grid>
</Page>
  Controls/FlyoutControl/PopupMenuDemo.xaml.cs



/*
* PopupMenu - 上下文菜单(未继承任何类)
*     Commands - 上下文菜单中的命令集合,返回 IList<IUICommand> 类型的数据
*     IAsyncOperation<IUICommand> ShowAsync(Point invocationPoint) - 在指定的位置(PopupMenu 的默认显示位置在窗口 0,0 点)上显示上下文菜单,并返回用户激发的命令
*     IAsyncOperation<IUICommand> 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);
}
}
}
}
  
OK
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389135-1-1.html 上篇帖子: Windows下检测文件名大小写是否匹配 下篇帖子: C#中Windows Media Player控件使用实例|方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表