我之前開發一個軟件 winMarkdown,這個軟件在關閉需要提示用戶還沒有保存東西,需要保存,如果用戶選擇退出,那么把數據存放。
在Metro程序中,沒有傳統的窗口,當我們要用需要交互的消息提示時,在Win8時代,引入了一個MessageDialog來取代常用的MessageBox。
我在MainPage,掛起`App.Current.Suspending += suspend;`
~~~
private async void suspend(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral();
MessageDialog message_dialog = new MessageDialog("當前還在運行,確定退出", "退出");
message_dialog.Commands.Add(new UICommand("確定", cmd => { }, "退出"));
message_dialog.Commands.Add(new UICommand("取消", cmd => { }));
message_dialog.DefaultCommandIndex = 0;
message_dialog.CancelCommandIndex = 1;
IUICommand result = await message_dialog.ShowAsync();
if (result.Id as string == "退出")
{
}
deferral.Complete();
}
~~~
`SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral();`掛起還要做,直到`deferral.Complete();`
~~~
MessageDialog message_dialog = new MessageDialog("當前還在運行,確定退出", "退出");
message_dialog.Commands.Add(new UICommand("確定", cmd => { }, "退出"));
message_dialog.Commands.Add(new UICommand("取消", cmd => { }));
~~~
兩個按鈕,一個確定,一個取消,可以UICommand ID作為點擊后,是哪個按鈕點擊
~~~
MessageDialog.DefaultCommandIndex按ESC選擇按鈕
MessageDialog.CancelCommandIndex按enter按鈕
~~~
~~~
IUICommand result = await message_dialog.ShowAsync();
if (result.Id as string == "退出")
{
}
~~~
程序要調試掛起,需要生命周期,點擊掛起?

我們按enter就會點擊確定
而我們對于MessageDialog功能還是覺得不夠,ContentDialog可以定義復雜的Xaml自定義
我們把MessageDialog換ContentDialog
~~~
ContentDialog content_dialog = new ContentDialog()
{
Title = "退出",
Content = "當前還在運行,確定退出",
PrimaryButtonText = "確定",
SecondaryButtonText = "取消",
FullSizeDesired = true,
};
content_dialog.PrimaryButtonClick += (_s, _e) => { };
await content_dialog.ShowAsync();
~~~

~~~
<UserControl
x:Class="produproperty.content"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:produproperty"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="當前還在運行,確定退出"></TextBlock>
<CheckBox Grid.Row="1" Content="保存"></CheckBox>
</Grid>
</UserControl>
~~~
~~~
ContentDialog content_dialog = new ContentDialog()
{
Title = "退出",
Content = new content(),
PrimaryButtonText = "確定",
SecondaryButtonText = "取消",
FullSizeDesired = false,
};
content_dialog.PrimaryButtonClick += (_s, _e) => { };
await content_dialog.ShowAsync();
~~~

參見:?
[http://www.cnblogs.com/TianFang/p/4857205.html](http://www.cnblogs.com/TianFang/p/4857205.html)
- 前言
- UWP win10 app 新關鍵字x:Bing
- win10應用 UWP 使用MD5算法
- win10 UWP讀寫文件
- UWP appButtonBar樣式
- C# 6.0 $&quot;Hello {csdn}&quot;
- Win10 UWP xaml 延遲加載元素
- UWP xaml 圓形頭像
- UWP 繪制圖形
- win10 uwp 通知Toast
- win10 UWP 顯示地圖
- win10 uwp 參考
- win10 uwp clone
- win10 uwp 裝機必備應用 含源代碼
- RichEditBox 使用自定義菜單
- win10 UWP FlipView
- win10 UWP 獲取系統信息
- win10 UWP 申請微軟開發者
- win10 UWP button
- win10 UWP Markdown 含源代碼
- win10 UWP 應用設置
- win10 UWP 九幽數據分析
- win10 UWP 圓形等待
- win10 UWP 標題欄后退
- win10 UWP 單元測試
- win10 UWP 你寫我讀
- win10 UWP RSS閱讀器
- win10 UWP MessageDialog 和 ContentDialog
- win10 UWP Hmac
- win10 UWP GET Post
- Win10 UWP Intro to controls and events
- win10 UWP Controls by function
- win10 uwp App-to-app communication 應用通信