• WP8.1StoreApp(WP8.1RT)---MessageBox与MessageDialog


    在WP7和WP8中,MessageBox是跟WinForm中一样常用的对话框,但是有一个显著的缺点,就是WP7/8中默认的MessageBox是阻塞线程的。也许是由于这个原因,WP8.1/Win8中采用了异步的MessageDialog对话框, 其扩展性和可定制性更强。但是在很多情况下需要挂起线程等待用户响应或者是单纯怀念MessageBox的简单方便的使用方式。于是我封装了一下WP8.1中的MessageDialog,使其可以像MessageBox一样简单的使用。

    public sealed class MessageBox
    {
        // 摘要: 
        //     显示包含指定文本和“确定”按钮的消息框。
        //
        // 参数: 
        //   messageBoxText:
        //     要显示的消息。
        //
        // 异常: 
        //   System.ArgumentNullException:
        //     messageBoxText 为 null。
        public async static Task<MessageBoxResult> Show(string messageBoxText)
        {
            if (messageBoxText == null)
                throw new ArgumentNullException("messageBoxText is null");
     
            var tcs = new TaskCompletionSource<MessageBoxResult>();
     
            var dialog = new MessageDialog(messageBoxText);
     
            dialog.Commands.Add(new UICommand("确定", command =>
            {
                tcs.SetResult((MessageBoxResult)command.Id);
            }, MessageBoxResult.OK));
     
            dialog.DefaultCommandIndex = 0;
            dialog.CancelCommandIndex = 0;
     
            await dialog.ShowAsync();
     
            return await tcs.Task;
        }
     
        //
        // 摘要: 
        //     显示包含指定文本、标题栏标题和响应按钮的消息框。
        //
        // 参数: 
        //   messageBoxText:
        //     要显示的消息。
        //
        //   caption:
        //     消息框的标题。
        //
        //   button:
        //     一个值,用于指示要显示哪个按钮或哪些按钮。
        //
        // 返回结果: 
        //     一个值,用于指示用户对消息的响应。
        //
        // 异常: 
        //   System.ArgumentNullException:
        //     messageBoxText 为 null。- 或 -caption 为 null。
        //
        //   System.ArgumentException:
        //     button 不是有效的 System.Windows.MessageBoxButton 值。
        public async static Task<MessageBoxResult> Show(string messageBoxText, string caption, MessageBoxButton button)
        {
            if (messageBoxText == null)
                throw new ArgumentNullException("messageBoxText is null");
            if (caption == null)
                throw new ArgumentNullException("caption is null");
            if (button != MessageBoxButton.OK && button != MessageBoxButton.OKCancel)
                throw new ArgumentException("button is null");
     
            var tcs = new TaskCompletionSource<MessageBoxResult>();
     
            var dialog = new MessageDialog(messageBoxText,caption);
     
            dialog.Commands.Add(new UICommand("确定", command =>
            {
                tcs.SetResult((MessageBoxResult)command.Id);
            }, MessageBoxResult.OK));
     
            if (button==MessageBoxButton.OKCancel)
            {
                dialog.Commands.Add(new UICommand("取消", command =>
                {
                    tcs.SetResult((MessageBoxResult)command.Id);
                }, MessageBoxResult.Cancel));
            }
     
            dialog.DefaultCommandIndex = 0;
            if(button==MessageBoxButton.OKCancel)
                dialog.CancelCommandIndex = 1;
            else
                dialog.CancelCommandIndex = 0;
     
            await dialog.ShowAsync();
     
            return await tcs.Task;
        }
    }
     
    // 摘要: 
    //     指定显示消息框时要包含的按钮。
    public enum MessageBoxButton
    {
        // 摘要: 
        //     仅显示“确定”按钮。
        OK = 0,
        //
        // 摘要: 
        //     同时显示“确定”和“取消”按钮。
        OKCancel = 1,
    }
     
    // 摘要: 
    //     表示对消息框的用户响应。
    public enum MessageBoxResult
    {
        // 摘要: 
        //     当前未使用此值。
        None = 0,
        //
        // 摘要: 
        //     用户单击了“确定”按钮。
        OK = 1,
        //
        // 摘要: 
        //     用户单击了“取消”按钮或按下了 ESC。
        Cancel = 2,
        //
        // 摘要: 
        //     当前未使用此值。
        Yes = 6,
        //
        // 摘要: 
        //     当前未使用此值。
        No = 7,
    }

    调用方式:

    await MessageBox.Show("demo");
    
    await MessageBox.Show("demo", "liubaicai.com", MessageBoxButton.OKCancel);
     
    MessageBoxResult result = await MessageBox.Show("demo", "liubaicai.com", MessageBoxButton.OKCancel);

    收工
    http://www.liubaicai.net/?p=298

  • 相关阅读:
    windows下搭建基于eclipse插件的GoLang开发环境
    vtk类之vtkImageReslice:基本算法,对体数据沿着轴进行切片
    vtk类之vtkTextureMapToSphere:纹理映射算法, 映射球体纹理
    wxpython 之 GDI 画刷Brush(三)
    c# 获取Rss数据
    C# 基础知识系列之面向对象基础
    SilverLight 得到文件绝对路径
    SilverLight C# 读取并修改App.config文件
    Silverlight 脱离浏览器
    C# 中New关键字的用法
  • 原文地址:https://www.cnblogs.com/liubaicai/p/3669849.html
Copyright © 2020-2023  润新知