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

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

[复制链接]

尚未签到

发表于 2017-6-28 14:18:25 | 显示全部楼层 |阅读模式
  [源码下载]




背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog  
作者:webabcd

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


  • MessageDialog
  • ContentDialog
  
示例
1、MessageDialog 的示例
Controls/FlyoutControl/MessageDialogDemo.xaml



<Page
x:Class="Windows10.Controls.FlyoutControl.MessageDialogDemo"
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" />
<Button Name="btnShowMessageDialogSimple" Margin="5" Content="弹出一个简单的 MessageDialog" Click="btnShowMessageDialogSimple_Click" />
<Button Name="btnShowMessageDialogCustomCommand" Margin="5" Content="弹出一个自定义命令按钮的 MessageDialog" Click="btnShowMessageDialogCustomCommand_Click" />
</StackPanel>
</Grid>
</Page>
  Controls/FlyoutControl/MessageDialogDemo.xaml.cs



/*
* MessageDialog - 信息对话框(未继承任何类)
*     Content - 内容
*     Title - 标题
*     Options - 选项(Windows.UI.Popups.MessageDialogOptions 枚举)
*         None - 正常,默认值
*         AcceptUserInputAfterDelay - 为避免用户误操作,弹出对话框后短时间内禁止单击命令按钮
*     Commands - 对话框的命令栏中的命令集合,返回 IList<IUICommand> 类型的数据
*     DefaultCommandIndex - 按“enter”键后,激发此索引位置的命令
*     CancelCommandIndex - 按“esc”键后,激发此索引位置的命令
*     ShowAsync() - 显示对话框,并返回用户激发的命令
*     
* IUICommand - 命令
*     Label - 显示的文字
*     Id - 参数
*/
using System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Windows10.Controls.FlyoutControl
{
public sealed partial class MessageDialogDemo : Page
{
public MessageDialogDemo()
{
this.InitializeComponent();
}
// 弹出一个简单的 MessageDialog
private async void btnShowMessageDialogSimple_Click(object sender, RoutedEventArgs e)
{
MessageDialog messageDialog = new MessageDialog("内容");
await messageDialog.ShowAsync();
}
// 弹出一个自定义命令按钮的 MessageDialog
private async void btnShowMessageDialogCustomCommand_Click(object sender, RoutedEventArgs e)
{
MessageDialog messageDialog = new MessageDialog("内容", "标题");
messageDialog.Commands.Add
(
new UICommand
(
"自定义命令按钮1",
(command) =>
{
lblMsg.Text = string.Format("command label:{0}, id:{1}", command.Label, command.Id);
},
"param1"
)
);
messageDialog.Commands.Add
(
new UICommand
(
"自定义命令按钮2",
(command) =>
{
lblMsg.Text = string.Format("command label:{0}, id:{1}", command.Label, command.Id);
},
"param2"
)
);
messageDialog.Commands.Add
(
new UICommand
(
"自定义命令按钮3",
(command) =>
{
lblMsg.Text = string.Format("command label:{0}, id:{1}", command.Label, command.Id);
},
"param3"
)
);
messageDialog.DefaultCommandIndex = 0; // 按“enter”键后,激发第 1 个命令
messageDialog.CancelCommandIndex = 2; // 按“esc”键后,激发第 3 个命令
messageDialog.Options = MessageDialogOptions.AcceptUserInputAfterDelay; // 对话框弹出后,短时间内禁止用户单击命令按钮,以防止用户的误操作
// 显示对话框,并返回用户激发的命令
IUICommand chosenCommand = await messageDialog.ShowAsync();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += string.Format("result label:{0}, id:{1}", chosenCommand.Label, chosenCommand.Id);
}
}
}
  
2、ContentDialog 的示例
Controls/FlyoutControl/CustomContentDialog.xaml



<ContentDialog
x:Class="Windows10.Controls.FlyoutControl.CustomContentDialog"
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"
Title="custom dialog title"
PrimaryButtonText="primary button"  
SecondaryButtonText="secondary button"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<!--以下为自定义对话框的标题-->
<ContentDialog.TitleTemplate>
<DataTemplate>
<TextBlock Text="custom dialog title" Foreground="Red" />
</DataTemplate>
</ContentDialog.TitleTemplate>
<!--以下为自定义对话框的内容-->
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TextBox Name="email" Header="Email address"/>
<PasswordBox  Name="password" Header="Password"/>
<CheckBox Name="showPassword" Content="Show password"/>
<TextBlock Name="body" TextWrapping="Wrap">
<TextBlock.Text>
content content content content content content content content content content content content content content
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ContentDialog>
  Controls/FlyoutControl/CustomContentDialog.xaml.cs



/*
* 自定义 ContentDialog
*/
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
namespace Windows10.Controls.FlyoutControl
{
public sealed partial class CustomContentDialog : ContentDialog
{
public CustomContentDialog()
{
this.InitializeComponent();
}
// 用户点击了第一个按钮
private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
// 通过 GetDeferral() 来等待长时任务,否则即使 await 了也不会等
ContentDialogButtonClickDeferral deferral = args.GetDeferral();
try
{
await Task.Delay(1);
}
finally
{
// 完成异步操作
                deferral.Complete();
}
// 使此事件可以冒泡(当然 args.Cancel 默认就是 false)
args.Cancel = false;
}
// 用户点击了第二个按钮
private async void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
// 通过 GetDeferral() 来等待长时任务,否则即使 await 了也不会等
ContentDialogButtonClickDeferral deferral = args.GetDeferral();
try
{
await Task.Delay(1);
}
finally
{
// 完成异步操作
                deferral.Complete();
}
// 使此事件可以冒泡(当然 args.Cancel 默认就是 false)
args.Cancel = false;
}
}
}
  Controls/FlyoutControl/ContentDialogDemo.xaml



<Page
x:Class="Windows10.Controls.FlyoutControl.ContentDialogDemo"
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">
<Button Name="btnShowDialog" Margin="5" Content="弹出一个标准的对话框" Click="btnShowDialog_Click" />
<Button Name="btnShowCustomDialog" Margin="5" Content="弹出一个自定义的对话框" Click="btnShowCustomDialog_Click" />
<TextBlock Name="lblMsg" Margin="5" />
</StackPanel>
</Grid>
</Page>
  Controls/FlyoutControl/ContentDialogDemo.xaml.cs



/*
* ContentDialog - 内容对话框(继承自 ContentControl, 请参见 /Controls/BaseControl/ContentControlDemo/)
*     FullSizeDesired - 是否全屏弹出对话框
*     Title, TitleTemplate - 对话框的标题(可以自定义样式)
*     Content, ContentTemplate - 对话框的内容(可以自定义样式)
*     PrimaryButtonText - 对话框第一个按钮显示的文本
*     SecondaryButtonText - 对话框第二个按钮显示的文本
*     PrimaryButtonCommand, PrimaryButtonCommandParameter, SecondaryButtonCommand, SecondaryButtonCommandParameter - 按钮命令及命令参数
*     
*     PrimaryButtonClick - 第一个按钮按下时触发的事件
*     SecondaryButtonClick - 第二个按钮按下时触发的事件
*     Closed, Closing, Opened - 顾名思义的一些事件
*     
*     ShowAsync() - 弹出对话框
*     Hide() - 隐藏对话框
*     
*
* 注意:自定义的内容对话框参见 CustomContentDialog.xaml
*/
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Windows10.Controls.FlyoutControl
{
public sealed partial class ContentDialogDemo : Page
{
public ContentDialogDemo()
{
this.InitializeComponent();
}
private async void btnShowDialog_Click(object sender, RoutedEventArgs e)
{
ContentDialog dialog = new ContentDialog()
{
Title = "dialog title",
Content = "dialog content, dialog content, dialog content, dialog content, dialog content, dialog content, dialog content",
FullSizeDesired = true,
PrimaryButtonText = "PrimaryButton",
SecondaryButtonText = "SecondaryButton"
};
dialog.PrimaryButtonClick += dialog_PrimaryButtonClick;
dialog.SecondaryButtonClick += dialog_SecondaryButtonClick;
// 弹出对话框,返回值为用户的选择结果
/*
* ContentDialogResult.Primary - 用户选择了第一个按钮
* ContentDialogResult.Secondary - 用户选择了第二个按钮
* ContentDialogResult.None - 用户没有选择(按了系统的“返回”按钮)
*/
ContentDialogResult result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
lblMsg.Text += "选择了第一个按钮";
lblMsg.Text += Environment.NewLine;
}
else if (result == ContentDialogResult.Secondary)
{
lblMsg.Text += "选择了第二个按钮";
lblMsg.Text += Environment.NewLine;
}
else if (result == ContentDialogResult.None)
{
lblMsg.Text += "没有选择按钮";
lblMsg.Text += Environment.NewLine;
}
}
void dialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
lblMsg.Text += "点击了第一个按钮";
lblMsg.Text += Environment.NewLine;
}
void dialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
lblMsg.Text += "点击了第二个按钮";
lblMsg.Text += Environment.NewLine;
}

// 弹出自定义对话框
async private void btnShowCustomDialog_Click(object sender, RoutedEventArgs e)
{
// 实例化自定义对话框
CustomContentDialog contentDialog = new CustomContentDialog();
// 弹出对话框,返回值为用户的选择结果
/*
* ContentDialogResult.Primary - 用户选择了第一个按钮
* ContentDialogResult.Secondary - 用户选择了第二个按钮
* ContentDialogResult.None - 用户没有选择(按了系统的“返回”按钮)
*/
ContentDialogResult result = await contentDialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
lblMsg.Text += "选择了第一个按钮";
lblMsg.Text += Environment.NewLine;
}
else if (result == ContentDialogResult.Secondary)
{
lblMsg.Text += "选择了第二个按钮";
lblMsg.Text += Environment.NewLine;
}
else if (result == ContentDialogResult.None)
{
lblMsg.Text += "没有选择按钮";
lblMsg.Text += Environment.NewLine;
}
}
}
}
  
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-389026-1-1.html 上篇帖子: Windows环境Mycat数据库分库分表中间件部署 下篇帖子: Windows Visual Studio 配置Clang
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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