messagebox_AvaloniaUI的MessageBox

messagebox

Let's try to do it.

让我们尝试去做。

Solution, which I wanna improve and support, you can find on nuget and on gitlab. It'll be glad, if somebody bring new ideas with his/her pull requests.

我想改进和支持的解决方案,可以在nugetgitlab找到 。 如果有人提出自己的要求,会很高兴。

I wanna create my lib as easy and useful as WPF's messagebox is.

我想创建自己的库,就像WPF的消息框一样容易和有用。

开始吧 (Let's start)

Call this method:

调用此方法:

MessageBox.ShowForResult("test","Wanna test smth?",MessageBox.MessageBoxButtons.OkCancel);

and get window with header, content(text) and two buttons. (Windows 10, Ubuntu 18.04)

并获得带有标题,内容(文本)和两个按钮的窗口。 (Windows 10,Ubuntu 18.04)

Class MessageBox contains public 4 methods:

MessageBox类包含4种公共方法:

ShowForResult — return result of button's click.

ShowForResult —返回按钮单击的结果。

ShowDialog — return result of button's click and set this window as dialog window for chosen parent.

ShowDialog —返回按钮单击的结果,并将此窗口设置为所选父项的对话框窗口。

Show — show window with one «ok» button, which ignore result of click (return void).

显示—显示带有一个“确定”按钮的窗口,该按钮忽略单击结果(返回无效)。

ShowNative — try to call native window for platform, if failed, call ShowForResult.

ShowNative —尝试调用平台的本机窗口,如果失败,则调用ShowForResult。

API API

这个怎么运作: (How it works:)

In every method create window,

在每个方法创建窗口中,

var messageBox = new MessageBox();

and set content for each.

并设置每个内容。

messageBox.Content = CreateBaseMsgBox(text, buttons, messageBox);

Content is a grid, which include rows, one for textbox:

内容是一个网格,其中包括行,一个用于文本框:

var textBlock = new TextBlock();
 textBlock.Text = text;
 textBlock.TextAlignment = TextAlignment.Center;
 textBlock.TextWrapping = TextWrapping.Wrap;
 Grid.SetRow(textBlock,0);
 grid.Children.Add(textBlock);

other for grid with buttons:

其他用于带按钮的网格:

var btnGrid = GetButtonGrid(GetButton(window, MessageBoxResult.Yes),
 GetButton(window,MessageBoxResult.No));
 Grid.SetRow(btnGrid,1);
 grid.Children.Add(btnGrid);

GetButtonGrid的完整列表。 (Full listing for GetButtonGrid.)

private static Grid GetButtonGrid(params Button[] buttons)
        {
            var grid = new Grid();
            List<ColumnDefinition> definitions = new List<ColumnDefinition>();
            for (int i = 0; i < buttons.Length; i++)
            {
                definitions.Add(new ColumnDefinition{Width = new GridLength(5)});
                definitions.Add(new ColumnDefinition{Width = new GridLength(1,GridUnitType.Star)});
            }
            definitions.Add(new ColumnDefinition{Width = new GridLength(5)});
            grid.ColumnDefinitions.AddRange(definitions);
             var j = 1;
            foreach (var btn in buttons)
            {
                Grid.SetColumn(btn,j);
                j += 2;
                grid.Children.Add(btn);
            }
            return grid;
        }

The functionality of the buttons is given by the method:

按钮的功能由方法给出:

GetButton(MessageBox window,MessageBoxResult result)

阅读更多 (Read more)

private static Button GetButton(MessageBox window,MessageBoxResult result)
        {
           var btn = new Button();
           btn.Content = result.ToString();
           btn.Click += (_, __) =>
           {
               window.Res = result;
               window.Close();
           };
           return btn;
        }

The method accepts a window which be manipulated by buttons, and result returned by them.

该方法接受一个由按钮操纵的窗口,并由它们返回结果。

And, the last thing to consider is a code fragment that provides the result of a button click:

而且,最后要考虑的是一个代码片段,它提供了单击按钮的结果:

var tcs = new TaskCompletionSource<MessageBoxResult>();
messageBox.Closed += delegate { tcs.TrySetResult(messageBox.Res); };
...
return tcs.Task;

As a result, we get simple windows with buttons, which will allow us to create cross-platform MessageBox.

结果,我们得到带有按钮的简单窗口,这将使我们能够创建跨平台的MessageBox。

Grateful for user worldbeater.

感谢用户worldbeater

翻译自: https://habr.com/en/post/454386/

messagebox

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值