欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

messagebox_AvaloniaUI的MessageBox

程序员文章站 2022-07-16 13:19:55
...

messagebox

Let's try to do it.

让我们尝试去做。

messagebox_AvaloniaUI的MessageBox

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)

messagebox_AvaloniaUI的MessageBox
messagebox_AvaloniaUI的MessageBox

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