功能更新
This commit is contained in:
42
Melskin/Controls/Modal/Modal.cs
Normal file
42
Melskin/Controls/Modal/Modal.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace Melskin.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// 提供静态方法用于显示模态对话框,包括确认对话框和信息对话框。
|
||||
/// </summary>
|
||||
public static class Modal
|
||||
{
|
||||
/// <summary>
|
||||
/// 显示一个确认对话框
|
||||
/// </summary>
|
||||
/// <param name="owner">父窗口</param>
|
||||
/// <param name="title">标题</param>
|
||||
/// <param name="message">信息</param>
|
||||
/// <param name="onOkAsync">点击确定按钮时执行的异步委托</param>
|
||||
/// <returns>如果用户点击OK并异步操作成功,则返回true,否则返回false</returns>
|
||||
public static bool? Confirm(Window owner, string title, string message, Func<Task<bool>>? onOkAsync = null)
|
||||
{
|
||||
var modal = new ModalWindow(title, message)
|
||||
{
|
||||
Owner = owner,
|
||||
OnOkAsync = onOkAsync
|
||||
};
|
||||
return modal.ShowDialog();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示一个信息对话框
|
||||
/// </summary>
|
||||
/// <param name="owner">父窗口</param>
|
||||
/// <param name="title">标题</param>
|
||||
/// <param name="message">信息</param>
|
||||
public static void Info(Window owner, string title, string message)
|
||||
{
|
||||
var modal = new ModalWindow(title, message)
|
||||
{
|
||||
Owner = owner
|
||||
};
|
||||
// 隐藏取消按钮
|
||||
modal.CancelButton.Visibility = Visibility.Collapsed;
|
||||
modal.ShowDialog();
|
||||
}
|
||||
}
|
||||
102
Melskin/Controls/Modal/ModalWindow.xaml
Normal file
102
Melskin/Controls/Modal/ModalWindow.xaml
Normal file
@@ -0,0 +1,102 @@
|
||||
<Window
|
||||
AllowsTransparency="True"
|
||||
Background="Transparent"
|
||||
Height="220"
|
||||
Title="Modal"
|
||||
Width="400"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
WindowStyle="None"
|
||||
mc:Ignorable="d"
|
||||
x:Class="Melskin.Controls.ModalWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<Window.Resources>
|
||||
<Style TargetType="Button" x:Key="ModalButtonStyle">
|
||||
<Setter Property="Padding" Value="10,5" />
|
||||
<Setter Property="MinWidth" Value="70" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource TextPrimaryBrush}" />
|
||||
<Setter Property="Background" Value="{DynamicResource BackgroundFloatingBrush}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource BorderCornerLightedGradientBrush}" />
|
||||
<Setter Property="BorderThickness" Value="1" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="4">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
Margin="{TemplateBinding Padding}"
|
||||
VerticalAlignment="Center" />
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="Background" Value="#E0E0E0" />
|
||||
</Trigger>
|
||||
<Trigger Property="IsPressed" Value="True">
|
||||
<Setter Property="Background" Value="#D0D0D0" />
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Window.Resources>
|
||||
<Border
|
||||
Background="{DynamicResource BackgroundFloatingBrush}"
|
||||
BorderBrush="{DynamicResource BorderNormalBrush}"
|
||||
BorderThickness="1"
|
||||
CornerRadius="8">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Title -->
|
||||
<TextBlock
|
||||
FontSize="16"
|
||||
FontWeight="SemiBold"
|
||||
Foreground="{DynamicResource TextPrimaryBrush}"
|
||||
Grid.Row="0"
|
||||
Margin="15,10"
|
||||
Text="Dialog Title"
|
||||
x:Name="TitleTextBlock" />
|
||||
|
||||
<!-- Content -->
|
||||
<TextBlock
|
||||
Foreground="{DynamicResource TextPrimaryBrush}"
|
||||
Grid.Row="1"
|
||||
Margin="15,10"
|
||||
Text="This is the modal content."
|
||||
TextWrapping="Wrap"
|
||||
VerticalAlignment="Center"
|
||||
x:Name="MessageTextBlock" />
|
||||
|
||||
<!-- Buttons -->
|
||||
<StackPanel
|
||||
Grid.Row="2"
|
||||
HorizontalAlignment="Right"
|
||||
Margin="15,10"
|
||||
Orientation="Horizontal">
|
||||
<Button
|
||||
Click="CancelButton_Click"
|
||||
Content="取消"
|
||||
Style="{StaticResource ModalButtonStyle}"
|
||||
x:Name="CancelButton" />
|
||||
<Button
|
||||
Click="OkButton_Click"
|
||||
Content="确定"
|
||||
IsDefault="True"
|
||||
Margin="10,0,0,0"
|
||||
Style="{StaticResource ModalButtonStyle}"
|
||||
x:Name="OkButton" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Window>
|
||||
55
Melskin/Controls/Modal/ModalWindow.xaml.cs
Normal file
55
Melskin/Controls/Modal/ModalWindow.xaml.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
namespace Melskin.Controls;
|
||||
/// <summary>
|
||||
/// ModalWindow.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class ModalWindow : Window
|
||||
{
|
||||
// 用于异步操作
|
||||
/// <summary>
|
||||
/// 当用户点击确定按钮时触发的异步委托。此属性允许设置一个返回布尔值的任务,用于执行自定义逻辑。
|
||||
/// 如果设置了此属性,并且在用户点击确定按钮后,将调用该委托执行指定的异步操作。
|
||||
/// 根据委托返回的结果决定是否关闭对话框:如果返回true,则对话框以成功状态关闭;若返回false,则保持对话框打开。
|
||||
/// </summary>
|
||||
public Func<Task<bool>>? OnOkAsync { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 代表一个模态窗口,用于显示对话框。
|
||||
/// </summary>
|
||||
public ModalWindow(string title, string message)
|
||||
{
|
||||
InitializeComponent();
|
||||
TitleTextBlock.Text = title;
|
||||
MessageTextBlock.Text = message;
|
||||
}
|
||||
|
||||
private async void OkButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (OnOkAsync != null)
|
||||
{
|
||||
OkButton.IsEnabled = false;
|
||||
OkButton.Content = "加载中..."; // 模拟加载状态
|
||||
|
||||
var result = await OnOkAsync();
|
||||
if (result)
|
||||
{
|
||||
this.DialogResult = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 恢复按钮状态,停留在对话框
|
||||
OkButton.IsEnabled = true;
|
||||
OkButton.Content = "确定";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.DialogResult = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.DialogResult = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user