zoukankan      html  css  js  c++  java
  • SilverLight类似WinForm弹窗等待结果再继续执行

    在开发SilverLight时,弹窗一直都是用的回调方式,比如需要用户确认才能继续操作的,如果有好几个确认步骤,这时候回调函数就比较深了,代码基本看不懂,可以使用TaskCompletionSource把事件改为异步等待方法,全部改成同步的写法,爽的飞起。


    关键代码

    [Flags]
    public enum MsgBoxButton
    {
        Ok = 1,
        YesNo = 2,
        OkCancel = 4,
        YesNoCancel = 8,
    
        //图标
        IconInfo = 16,
        IconWarn = 32,
        IconQuestion = 64,
        IconError = 128,
    }
    
    public static Task<System.Windows.MessageBoxResult> ShowAsync(string message, string title, MsgBoxButton buttons)
    {
        var taskResult = new TaskCompletionSource<System.Windows.MessageBoxResult>();
        MsgBoxWindow messageBox = new MsgBoxWindow();//这是一个ChildWindow,只是自定义了一些样式和加了一些按钮:Yes、no、OK等,仿照winform
        messageBox.generateButtons(buttons);
        messageBox.Title = string.IsNullOrEmpty(title) ? "系统提示" : title;
        messageBox.Message = message;
        messageBox.MessageTextBlock.Width = twidth;
    
        messageBox.Closed += (ss, ee) =>
        {
            //异步等待关键代码,只有SetResult后,await才会继续执行
            taskResult.SetResult(messageBox._msgBoxResult);//根据点击按钮转换成了System.Windows.MessageBoxResult枚举结果
        };
        messageBox.Show();
        return taskResult.Task;
    }
    //创建按钮时在点击按钮事件中设置对应的结果
    private void createOkButton()
    {
        if (_okButton != null) return;
    
        _okButton = new Button
        {
            Content = "确定",
            Width = 75,
            Margin = new Thickness(2)
        };
        _okButton.Click += (sender, args) => { this._msgBoxResult = MessageBoxResult.OK; DialogResult = true; };
    }
    

    这样使用

    var result = await MsgBoxWindow.ShowAsync("点吧", "店不大", MsgBoxButton.YesNo);
    MessageBox.Show(result.ToString());
    var result2 = await MsgBoxWindow.ShowAsync("点吧2", "店不大2", MsgBoxButton.YesNo);
    MessageBox.Show(result2.ToString());
    

    再也不需要这样了

    MsgBoxWindow.Show("点吧", "店不大",  MsgBoxButton.YesNo, rs => {
        MessageBox.Show(rs.ToString());
        MsgBoxWindow.Show("点吧2", "店不大2", MsgBoxButton.YesNo, rs2 =>
        {
            MessageBox.Show(rs2.ToString());
        });
    });
    

    参考资料

  • 相关阅读:
    Trapping Rain Water
    Construct Binary Tree from Preorder and Inorder Traversal
    Flatten Binary Tree to Linked List
    Permutations II
    Unique Paths II
    Path Sum II
    Unique Binary Search Trees II
    evdev module-----uinput.py
    evdev module-----events.py
    evdev module-----device.py
  • 原文地址:https://www.cnblogs.com/missile/p/13208071.html
Copyright © 2011-2022 走看看