1. 程式人生 > >win10 UWP MessageDialog 和 ContentDialog

win10 UWP MessageDialog 和 ContentDialog

當前 php 還要 content value primary del conda cti

我之前開發一個軟件 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 => { }));

兩個button。一個確定,一個取消。能夠UICommand ID作為點擊後,是哪個button點擊

MessageDialog.DefaultCommandIndex按ESC選擇button
MessageDialog.CancelCommandIndex按enterbutton
            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

win10 UWP MessageDialog 和 ContentDialog