70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Harmonia.WinUI.Messaging;
|
|
|
|
public class WindowsMessageService : IMessageService
|
|
{
|
|
private static MainWindow MainWindow => App.ServiceProvider.GetRequiredService<MainWindow>();
|
|
|
|
public async Task<bool> ConfirmAsync(string title, string message)
|
|
{
|
|
ContentDialog dialog = new()
|
|
{
|
|
XamlRoot = MainWindow.Content.XamlRoot,
|
|
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
|
|
Title = title,
|
|
Content = message,
|
|
PrimaryButtonText = "Yes",
|
|
CloseButtonText = "No",
|
|
DefaultButton = ContentDialogButton.Primary
|
|
};
|
|
|
|
var result = await dialog.ShowAsync();
|
|
|
|
return result == ContentDialogResult.Primary;
|
|
}
|
|
|
|
public async Task<string> InputTextAsync(string title, string message, string defaultText = "")
|
|
{
|
|
TextBox inputTextBox = new()
|
|
{
|
|
Text = defaultText,
|
|
Margin = new Thickness(0, 10, 0, 0)
|
|
};
|
|
|
|
inputTextBox.SelectAll();
|
|
|
|
ContentDialog dialog = new()
|
|
{
|
|
XamlRoot = MainWindow.Content.XamlRoot,
|
|
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
|
|
Title = title,
|
|
Content = new StackPanel
|
|
{
|
|
Children =
|
|
{
|
|
new TextBlock { Text = message },
|
|
inputTextBox
|
|
}
|
|
},
|
|
PrimaryButtonText = "OK",
|
|
CloseButtonText = "Cancel",
|
|
DefaultButton = ContentDialogButton.Primary
|
|
};
|
|
|
|
ContentDialogResult result = await dialog.ShowAsync();
|
|
|
|
if (result == ContentDialogResult.Primary)
|
|
{
|
|
return inputTextBox.Text;
|
|
}
|
|
else
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
} |