【问题标题】:How to show a message box in AvaloniaUI (beta)如何在 AvaloniaUI(测试版)中显示消息框
【发布时间】:2019-09-06 10:41:32
【问题描述】:

我正在使用Avalonia,我正在尝试显示一个与 WinForms 的MessageBox.Show() 等效的消息框。我找到了正在为此请求 API 的 GitHub issue,但我想知道人们在此期间在做什么。

我们是否需要实现一个像消息框一样的窗口或用户控件并通过 ShowDialog 显示它?

【问题讨论】:

    标签: c# .net avaloniaui


    【解决方案1】:

    MessageBox.xaml

    <Window xmlns="https://github.com/avaloniaui"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
            x:Class="MsgBox.MessageBox" SizeToContent="WidthAndHeight" CanResize="False">
        <StackPanel HorizontalAlignment="Center">
            <TextBlock HorizontalAlignment="Center" Name="Text"/>
            <StackPanel HorizontalAlignment="Center" Orientation="Horizontal" Name="Buttons">
                <StackPanel.Styles>
                    <Style Selector="Button">
                        <Setter Property="Margin" Value="5"/>
                    </Style>
                </StackPanel.Styles>
    
            </StackPanel>
        </StackPanel>
    </Window>
    
    

    MessageBox.xaml.cs

    using System.Threading.Tasks;
    using Avalonia.Controls;
    using Avalonia.Interactivity;
    using Avalonia.Markup.Xaml;
    
    namespace MsgBox
    {
        class MessageBox : Window
        {
            public enum MessageBoxButtons
            {
                Ok,
                OkCancel,
                YesNo,
                YesNoCancel
            }
    
            public enum MessageBoxResult
            {
                Ok,
                Cancel,
                Yes,
                No
            }
    
            public MessageBox()
            {
                AvaloniaXamlLoader.Load(this);
            }
    
            public static Task<MessageBoxResult> Show(Window parent, string text, string title, MessageBoxButtons buttons)
            {
                var msgbox = new MessageBox()
                {
                    Title = title
                };
                msgbox.FindControl<TextBlock>("Text").Text = text;
                var buttonPanel = msgbox.FindControl<StackPanel>("Buttons");
    
                var res = MessageBoxResult.Ok;
    
                void AddButton(string caption, MessageBoxResult r, bool def = false)
                {
                    var btn = new Button {Content = caption};
                    btn.Click += (_, __) => { 
                        res = r;
                        msgbox.Close();
                    };
                    buttonPanel.Children.Add(btn);
                    if (def)
                        res = r;
                }
    
                if (buttons == MessageBoxButtons.Ok || buttons == MessageBoxButtons.OkCancel)
                    AddButton("Ok", MessageBoxResult.Ok, true);
                if (buttons == MessageBoxButtons.YesNo || buttons == MessageBoxButtons.YesNoCancel)
                {
                    AddButton("Yes", MessageBoxResult.Yes);
                    AddButton("No", MessageBoxResult.No, true);
                }
    
                if (buttons == MessageBoxButtons.OkCancel || buttons == MessageBoxButtons.YesNoCancel)
                    AddButton("Cancel", MessageBoxResult.Cancel, true);
    
    
                var tcs = new TaskCompletionSource<MessageBoxResult>();
                msgbox.Closed += delegate { tcs.TrySetResult(res); };
                if (parent != null)
                    msgbox.ShowDialog(parent);
                else msgbox.Show();
                return tcs.Task;
            }
    
    
        }
    
    }
    

    用法:

    await  MessageBox.Show(mainWindow, "Test", "Test title", MessageBox.MessageBoxButtons.YesNoCancel)
    

    【讨论】:

    • 谢谢。由于某种原因,窗口被一个黑色方块淹没了,只有在我最大化和最小化它时它才被修复,但我猜这是另一个错误。窗口也有标准的窗口按钮,但删除这些按钮应该不难......我希望。
    • 请为这两个问题提交 github 问题。
    • 我不认为拥有标准按钮是一个错误。在我看来,一个 Window 对象默认应该有它的标准按钮。我通过将一些关于 Window 装饰的属性设置为 false 来隐藏它们(现在无法检查我在另一台计算机上)。此外,黑点可能仅处于调试模式。
    • kekekeks 谢谢,这太棒了! :D @Stilgar 如果你把它放在你的初始化代码中: this.SystemDecorations = SystemDecorations.BorderOnly;它会移除窗口装饰(显然,边框镀铬也......)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多