Avalonia MessageBox Input как получить текст с текстового поля

У меня есть такой код

  var box = MsBox.Avalonia.MessageBoxManager.GetMessageBoxCustom(
                new MessageBoxCustomParams()
                {
                    ContentTitle = "Введите имя задачи",
                    Width = 400,
                    Height = 120,
                    WindowStartupLocation = WindowStartupLocation.CenterOwner,
                    ButtonDefinitions = new List<ButtonDefinition>
                    {
                        new ButtonDefinition { Name = "Yes", },
                        new ButtonDefinition { Name = "No", }
                    },
                    InputParams = new InputParams()
                    {
                        Label = "Введите имя задачи:",
                        // DefaultValue = "dadada"
                    }
                });
            var result = box.ShowAsync();
Мне нужно получить текст с текстового поля, но result.Result возвращает только текст нажатой кнопки у MessageBox.


Ответы (2 шт):

Автор решения: Aziz Umarov

ну да дело на много проще у Вас на руках MsBox

public class MsBox<V, VM, T> : IMsBox<T> where V : UserControl, IFullApi<T>, ISetCloseAction where VM : ISetFullApi<T>, IInput
{
    private readonly V _view;
    private readonly VM _viewModel;

    public string InputValue { get { return _viewModel.InputValue; } }

    public MsBox(V view, VM viewModel)
    {
        _view = view;
        _viewModel = viewModel;
    }

    /// <summary>
    /// Show messagebox depending on the type of application
    /// If application is SingleViewApplicationLifetime (Mobile or Browses) then show messagebox as popup
    /// If application is ClassicDesktopStyleApplicationLifetime (Desktop) then show messagebox as window
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotSupportedException"></exception>
    public Task<T> ShowAsync()
    {
        if (Application.Current != null &&
            Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            return ShowWindowAsync();
        }

        if (Application.Current != null &&
            Application.Current.ApplicationLifetime is ISingleViewApplicationLifetime lifetime)
        {
            return ShowAsPopupAsync(lifetime.MainView as ContentControl);
        }

        throw new NotSupportedException("ApplicationLifetime is not supported");
    }

и по окончании ShowAsync загляните в InputValue

→ Ссылка
Автор решения: Alexandr_M
private async void Search_out_cross_Click(
    object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
    var box = MsBox.Avalonia.MessageBoxManager.GetMessageBoxCustom(
        new MessageBoxCustomParams()
        {
            ContentTitle = "Введите имя задачи",
            Width = 400,
            Height = 120,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            ButtonDefinitions = new List<ButtonDefinition>
            {
                new ButtonDefinition { Name = "Yes", },
                new ButtonDefinition { Name = "No", },
            },
            InputParams = new InputParams()
            {
                Label = "Введите имя задачи:",
            },

        });

    var result = await box.ShowAsync();
    Dispatcher.UIThread.Post(() =>
    {
        out_rich_cross.Text = box.InputValue; //передаём параметр из окна
    }, DispatcherPriority.Background);
}
→ Ссылка