[源码下载]

背水一战 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 = ; // 按“enter”键后,激发第 1 个命令
messageDialog.CancelCommandIndex = ; // 按“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();
}
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();
}
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
[源码下载]

背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog的更多相关文章

  1. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  2. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  3. 背水一战 Windows 10 (62) - 控件(媒体类): InkCanvas 保存和加载, 手写识别

    [源码下载] 背水一战 Windows 10 (62) - 控件(媒体类): InkCanvas 保存和加载, 手写识别 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) ...

  4. 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView

    [源码下载] 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView 作者:webabcd ...

  5. 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar

    [源码下载] 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) App ...

  6. 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画

    [源码下载] 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 动画 示例An ...

  7. 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker

    [源码下载] 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker 作者:webabcd 介绍背水一战 Window ...

  8. 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement

    [源码下载] 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) Im ...

  9. 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑

    [源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...

随机推荐

  1. JavaScript的前世今生

    和CSS一样,JavaScript在各浏览器下并非完全一致,它所带来的兼容性问题时常困扰着我们,以至于现在“能否处理流行浏览器的兼容性问题”成为了检验一个程序员是否合格的标准之一.了解JavaScri ...

  2. 编译Android AOSP代码

    下载完了源代码,终于到了编译的阶段了.这个阶段远比你想象的简单,一个make命令就可以完成源代码的编译了.参照下面的教程你就可以编译出适用于Android源代码树上的所有分支,包括master.基本的 ...

  3. Got the Best Employee of the year 2015 Star Award

    Got "The Best Employee of the year 2015 Star Award" from the company, thanks to all that h ...

  4. Hystrix框架5--请求缓存和collapser

    简介 在Hystrix中有个Request的概念,有一些操作需要在request中进行 缓存 在Hystrix调用服务时,如果只是查询接口,可以使用缓存进行优化,从而跳过真实访问请求. 应用 需要启用 ...

  5. VB6.0中,DTPicker日期、时间控件不允许为空时,采用文本框与日期、时间控件相互替换赋值(解决方案)

    VB6.0中,日期.时间控件不允许为空时,采用文本框与日期.时间控件相互替换赋值,或许是一个不错的选择. 实现效果如下图: 文本框txtStopTime1 时间框DTStopTime1(DTPicke ...

  6. ExtJs布局详解

    序言 1.百度百科上说:ExtJs功能丰富,无人能出其右.无论是界面之美,还是功能之强,extjs都高居榜首. 2.呵呵,界面之美当是少不了布局的,这篇文章我写layout的七种布局.(extjs是4 ...

  7. UDP(强行关闭了一个现有的连接远程主机)

    事件回顾:客户端连接服务器 ,一段时间后会发生服务器“挂掉”的情况,为了找到原因,在调试模式下运行服务器,捕捉到了一下异常: 红色框出来的即为异常原因:强行关闭了一个现有的连接远程主机 然后就发生了可 ...

  8. 【开源】OSharp框架解说系列(2.2):EasyUI复杂布局及数据操作

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  9. 【前端性能】必须要掌握的原生JS实现JQuery

    很多时候,我们经常听见有人说jquery有多快多快.在这个各种类库满天飞的时候,不得不说的是,能有原生JS快吗? 是的,明显原生JS要更快,因为诸如JQuery这样的库必须要兼容各种浏览器和低版本和许 ...

  10. MyCAT日志分析

    MyCAT日志对于了解MyCAT的运行信息不可获取,譬如MyCAT是否采用读写分离,对于一个查询语句,MyCAT是怎样执行的,每个分片会分发到哪个节点上等等. 默认是info级别,通过log4j.xm ...