Adjust project structure
This commit is contained in:
1 parent
89875792aa
commit
16ef430a39
82 files changed
+271
-10113
No files matched your search
@@ -1,601 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Interactivity;
|
||||
using HandyControl.Tools;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息提醒
|
||||
/// </summary>
|
||||
[TemplatePart(Name = ElementPanelMore, Type = typeof(Panel))]
|
||||
[TemplatePart(Name = ElementGridMain, Type = typeof(Grid))]
|
||||
[TemplatePart(Name = ElementButtonClose, Type = typeof(Button))]
|
||||
public class Growl : Control
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const string ElementPanelMore = "PART_PanelMore";
|
||||
private const string ElementGridMain = "PART_GridMain";
|
||||
private const string ElementButtonClose = "PART_ButtonClose";
|
||||
|
||||
#endregion Constants
|
||||
|
||||
#region Data
|
||||
|
||||
private Panel _panelMore;
|
||||
|
||||
private Grid _gridMain;
|
||||
|
||||
private Button _buttonClose;
|
||||
|
||||
private bool _showCloseButton;
|
||||
|
||||
private bool _staysOpen;
|
||||
|
||||
private int _waitTime = 6;
|
||||
|
||||
/// <summary>
|
||||
/// 计数
|
||||
/// </summary>
|
||||
private int _tickCount;
|
||||
|
||||
/// <summary>
|
||||
/// 关闭计时器
|
||||
/// </summary>
|
||||
private DispatcherTimer _timerClose;
|
||||
|
||||
private static readonly Dictionary<string, Panel> PanelDic = new Dictionary<string, Panel>();
|
||||
|
||||
#endregion Data
|
||||
|
||||
public Growl()
|
||||
{
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Close, ButtonClose_OnClick));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Cancel, ButtonCancel_OnClick));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, ButtonOk_OnClick));
|
||||
}
|
||||
|
||||
public static void Register(string token, Panel panel)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token) || panel == null) return;
|
||||
PanelDic[token] = panel;
|
||||
InitGrowlPanel(panel);
|
||||
}
|
||||
|
||||
public static void Unregister(string token, Panel panel)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token) || panel == null) return;
|
||||
|
||||
if (PanelDic.ContainsKey(token))
|
||||
{
|
||||
if (ReferenceEquals(PanelDic[token], panel))
|
||||
{
|
||||
PanelDic.Remove(token);
|
||||
panel.ContextMenu = null;
|
||||
panel.SetCurrentValue(PanelElement.FluidMoveBehaviorProperty, DependencyProperty.UnsetValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Unregister(Panel panel)
|
||||
{
|
||||
if (panel == null) return;
|
||||
var first = PanelDic.FirstOrDefault(item => ReferenceEquals(panel, item.Value));
|
||||
if (!string.IsNullOrEmpty(first.Key))
|
||||
{
|
||||
PanelDic.Remove(first.Key);
|
||||
panel.ContextMenu = null;
|
||||
panel.SetCurrentValue(PanelElement.FluidMoveBehaviorProperty, DependencyProperty.UnsetValue);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseEnter(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseEnter(e);
|
||||
|
||||
_buttonClose.Show(_showCloseButton);
|
||||
}
|
||||
|
||||
protected override void OnMouseLeave(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseLeave(e);
|
||||
|
||||
_buttonClose.Collapse();
|
||||
}
|
||||
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
base.OnApplyTemplate();
|
||||
|
||||
_panelMore = GetTemplateChild(ElementPanelMore) as Panel;
|
||||
_gridMain = GetTemplateChild(ElementGridMain) as Grid;
|
||||
_buttonClose = GetTemplateChild(ElementButtonClose) as Button;
|
||||
|
||||
CheckNull();
|
||||
Update();
|
||||
}
|
||||
|
||||
private void CheckNull()
|
||||
{
|
||||
if (_panelMore == null || _gridMain == null || _buttonClose == null) throw new Exception();
|
||||
}
|
||||
|
||||
private Func<bool, bool> ActionBeforeClose { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息容器
|
||||
/// </summary>
|
||||
public static Panel GrowlPanel { get; set; }
|
||||
|
||||
internal static readonly DependencyProperty CancelStrProperty = DependencyProperty.Register(
|
||||
"CancelStr", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
|
||||
|
||||
internal static readonly DependencyProperty ConfirmStrProperty = DependencyProperty.Register(
|
||||
"ConfirmStr", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
|
||||
|
||||
public static readonly DependencyProperty ShowDateTimeProperty = DependencyProperty.Register(
|
||||
"ShowDateTime", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.TrueBox));
|
||||
|
||||
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
|
||||
"Message", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
|
||||
|
||||
public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(
|
||||
"Time", typeof(DateTime), typeof(Growl), new PropertyMetadata(default(DateTime)));
|
||||
|
||||
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
|
||||
"Icon", typeof(Geometry), typeof(Growl), new PropertyMetadata(default(Geometry)));
|
||||
|
||||
public static readonly DependencyProperty IconBrushProperty = DependencyProperty.Register(
|
||||
"IconBrush", typeof(Brush), typeof(Growl), new PropertyMetadata(default(Brush)));
|
||||
|
||||
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
|
||||
"Type", typeof(InfoType), typeof(Growl), new PropertyMetadata(default(InfoType)));
|
||||
|
||||
public static readonly DependencyProperty GrowlParentProperty = DependencyProperty.RegisterAttached(
|
||||
"GrowlParent", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox, (o, args) =>
|
||||
{
|
||||
if ((bool)args.NewValue && o is Panel panel)
|
||||
{
|
||||
SetGrowlPanel(panel);
|
||||
}
|
||||
}));
|
||||
|
||||
public static readonly DependencyProperty TokenProperty = DependencyProperty.RegisterAttached(
|
||||
"Token", typeof(string), typeof(Growl), new PropertyMetadata(default(string), OnTokenChanged));
|
||||
|
||||
private static void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is Panel panel)
|
||||
{
|
||||
if (e.NewValue == null)
|
||||
{
|
||||
Unregister(panel);
|
||||
}
|
||||
else
|
||||
{
|
||||
Register(e.NewValue.ToString(), panel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetToken(DependencyObject element, string value)
|
||||
=> element.SetValue(TokenProperty, value);
|
||||
|
||||
public static string GetToken(DependencyObject element)
|
||||
=> (string)element.GetValue(TokenProperty);
|
||||
|
||||
public static void SetGrowlParent(DependencyObject element, bool value) => element.SetValue(GrowlParentProperty, value);
|
||||
|
||||
public static bool GetGrowlParent(DependencyObject element) => (bool)element.GetValue(GrowlParentProperty);
|
||||
|
||||
public InfoType Type
|
||||
{
|
||||
get => (InfoType)GetValue(TypeProperty);
|
||||
set => SetValue(TypeProperty, value);
|
||||
}
|
||||
|
||||
internal string CancelStr
|
||||
{
|
||||
get => (string)GetValue(CancelStrProperty);
|
||||
set => SetValue(CancelStrProperty, value);
|
||||
}
|
||||
|
||||
internal string ConfirmStr
|
||||
{
|
||||
get => (string)GetValue(ConfirmStrProperty);
|
||||
set => SetValue(ConfirmStrProperty, value);
|
||||
}
|
||||
|
||||
public bool ShowDateTime
|
||||
{
|
||||
get => (bool)GetValue(ShowDateTimeProperty);
|
||||
set => SetValue(ShowDateTimeProperty, value);
|
||||
}
|
||||
|
||||
public string Message
|
||||
{
|
||||
get => (string)GetValue(MessageProperty);
|
||||
set => SetValue(MessageProperty, value);
|
||||
}
|
||||
|
||||
public DateTime Time
|
||||
{
|
||||
get => (DateTime)GetValue(TimeProperty);
|
||||
set => SetValue(TimeProperty, value);
|
||||
}
|
||||
|
||||
public Geometry Icon
|
||||
{
|
||||
get => (Geometry)GetValue(IconProperty);
|
||||
set => SetValue(IconProperty, value);
|
||||
}
|
||||
|
||||
public Brush IconBrush
|
||||
{
|
||||
get => (Brush)GetValue(IconBrushProperty);
|
||||
set => SetValue(IconBrushProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始计时器
|
||||
/// </summary>
|
||||
private void StartTimer()
|
||||
{
|
||||
_timerClose = new DispatcherTimer
|
||||
{
|
||||
Interval = TimeSpan.FromSeconds(1)
|
||||
};
|
||||
_timerClose.Tick += delegate
|
||||
{
|
||||
if (IsMouseOver)
|
||||
{
|
||||
_tickCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
_tickCount++;
|
||||
if (_tickCount >= _waitTime) Close();
|
||||
};
|
||||
_timerClose.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 消息容器
|
||||
/// </summary>
|
||||
/// <param name="panel"></param>
|
||||
private static void SetGrowlPanel(Panel panel)
|
||||
{
|
||||
GrowlPanel = panel;
|
||||
InitGrowlPanel(panel);
|
||||
}
|
||||
|
||||
private static void InitGrowlPanel(Panel panel)
|
||||
{
|
||||
var menuItem = new MenuItem
|
||||
{
|
||||
Header = Properties.Langs.Lang.Clear
|
||||
};
|
||||
menuItem.Click += (s, e) =>
|
||||
{
|
||||
foreach (var item in panel.Children.OfType<Growl>())
|
||||
{
|
||||
item.Close();
|
||||
}
|
||||
};
|
||||
panel.ContextMenu = new ContextMenu
|
||||
{
|
||||
Items =
|
||||
{
|
||||
menuItem
|
||||
}
|
||||
};
|
||||
|
||||
PanelElement.SetFluidMoveBehavior(panel, ResourceHelper.GetResource<FluidMoveBehavior>(ResourceToken.BehaviorXY400));
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (ActionBeforeClose != null)
|
||||
{
|
||||
_staysOpen = true;
|
||||
_showCloseButton = false;
|
||||
_panelMore.IsEnabled = true;
|
||||
_panelMore.Show();
|
||||
}
|
||||
|
||||
var transform = new TranslateTransform
|
||||
{
|
||||
X = MaxWidth
|
||||
};
|
||||
_gridMain.RenderTransform = transform;
|
||||
transform.BeginAnimation(TranslateTransform.XProperty, AnimationHelper.CreateAnimation(0));
|
||||
if (!_staysOpen) StartTimer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示信息
|
||||
/// </summary>
|
||||
/// <param name="growlInfo"></param>
|
||||
private static void Show(GrowlInfo growlInfo)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
var ctl = new Growl
|
||||
{
|
||||
Message = growlInfo.Message,
|
||||
Time = DateTime.Now,
|
||||
Icon = ResourceHelper.GetResource<Geometry>(growlInfo.IconKey),
|
||||
IconBrush = ResourceHelper.GetResource<Brush>(growlInfo.IconBrushKey),
|
||||
_showCloseButton = growlInfo.ShowCloseButton,
|
||||
ActionBeforeClose = growlInfo.ActionBeforeClose,
|
||||
_staysOpen = growlInfo.StaysOpen,
|
||||
ShowDateTime = growlInfo.ShowDateTime,
|
||||
ConfirmStr = growlInfo.ConfirmStr,
|
||||
CancelStr = growlInfo.CancelStr,
|
||||
Type = growlInfo.Type,
|
||||
_waitTime = Math.Max(growlInfo.WaitTime, 2)
|
||||
};
|
||||
if (!string.IsNullOrEmpty(growlInfo.Token))
|
||||
{
|
||||
if (PanelDic.TryGetValue(growlInfo.Token, out var panel))
|
||||
{
|
||||
panel?.Children.Insert(0, ctl);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GrowlPanel.Children.Insert(0, ctl);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 成功
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="token"></param>
|
||||
public static void Success(string message, string token = "") => Success(new GrowlInfo
|
||||
{
|
||||
Message = message,
|
||||
Token = token
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 成功
|
||||
/// </summary>
|
||||
/// <param name="growlInfo"></param>
|
||||
public static void Success(GrowlInfo growlInfo)
|
||||
{
|
||||
if (growlInfo == null) throw new ArgumentNullException(nameof(growlInfo));
|
||||
if (!growlInfo.IsCustom)
|
||||
{
|
||||
growlInfo.IconKey = ResourceToken.SuccessGeometry;
|
||||
growlInfo.IconBrushKey = ResourceToken.SuccessBrush;
|
||||
growlInfo.Type = InfoType.Success;
|
||||
}
|
||||
Show(growlInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 消息
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="token"></param>
|
||||
public static void Info(string message, string token = "") => Info(new GrowlInfo
|
||||
{
|
||||
Message = message,
|
||||
Token = token
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 消息
|
||||
/// </summary>
|
||||
/// <param name="growlInfo"></param>
|
||||
public static void Info(GrowlInfo growlInfo)
|
||||
{
|
||||
if (growlInfo == null) throw new ArgumentNullException(nameof(growlInfo));
|
||||
if (!growlInfo.IsCustom)
|
||||
{
|
||||
growlInfo.IconKey = ResourceToken.InfoGeometry;
|
||||
growlInfo.IconBrushKey = ResourceToken.InfoBrush;
|
||||
growlInfo.Type = InfoType.Info;
|
||||
}
|
||||
Show(growlInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 警告
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="token"></param>
|
||||
public static void Warning(string message, string token = "") => Warning(new GrowlInfo
|
||||
{
|
||||
Message = message,
|
||||
Token = token
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 警告
|
||||
/// </summary>
|
||||
/// <param name="growlInfo"></param>
|
||||
public static void Warning(GrowlInfo growlInfo)
|
||||
{
|
||||
if (growlInfo == null) throw new ArgumentNullException(nameof(growlInfo));
|
||||
if (!growlInfo.IsCustom)
|
||||
{
|
||||
growlInfo.IconKey = ResourceToken.WarningGeometry;
|
||||
growlInfo.IconBrushKey = ResourceToken.WarningBrush;
|
||||
growlInfo.Type = InfoType.Warning;
|
||||
}
|
||||
Show(growlInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 错误
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="token"></param>
|
||||
public static void Error(string message, string token = "") => Error(new GrowlInfo
|
||||
{
|
||||
Message = message,
|
||||
Token = token
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 错误
|
||||
/// </summary>
|
||||
/// <param name="growlInfo"></param>
|
||||
public static void Error(GrowlInfo growlInfo)
|
||||
{
|
||||
if (growlInfo == null) throw new ArgumentNullException(nameof(growlInfo));
|
||||
if (!growlInfo.IsCustom)
|
||||
{
|
||||
growlInfo.IconKey = ResourceToken.ErrorGeometry;
|
||||
growlInfo.IconBrushKey = ResourceToken.DangerBrush;
|
||||
growlInfo.StaysOpen = true;
|
||||
growlInfo.Type = InfoType.Error;
|
||||
}
|
||||
Show(growlInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 严重
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="token"></param>
|
||||
public static void Fatal(string message, string token = "") => Fatal(new GrowlInfo
|
||||
{
|
||||
Message = message,
|
||||
Token = token
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 严重
|
||||
/// </summary>
|
||||
/// <param name="growlInfo"></param>
|
||||
public static void Fatal(GrowlInfo growlInfo)
|
||||
{
|
||||
if (growlInfo == null) throw new ArgumentNullException(nameof(growlInfo));
|
||||
if (!growlInfo.IsCustom)
|
||||
{
|
||||
growlInfo.IconKey = ResourceToken.FatalGeometry;
|
||||
growlInfo.IconBrushKey = ResourceToken.PrimaryTextBrush;
|
||||
growlInfo.StaysOpen = true;
|
||||
growlInfo.ShowCloseButton = false;
|
||||
growlInfo.Type = InfoType.Fatal;
|
||||
if (GrowlPanel.ContextMenu != null) GrowlPanel.ContextMenu.Opacity = 0;
|
||||
}
|
||||
Show(growlInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 询问
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="actionBeforeClose"></param>
|
||||
/// <param name="token"></param>
|
||||
public static void Ask(string message, Func<bool, bool> actionBeforeClose, string token = "") => Ask(new GrowlInfo
|
||||
{
|
||||
Message = message,
|
||||
ActionBeforeClose = actionBeforeClose,
|
||||
Token = token
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 询问
|
||||
/// </summary>
|
||||
/// <param name="growlInfo"></param>
|
||||
public static void Ask(GrowlInfo growlInfo)
|
||||
{
|
||||
if (growlInfo == null) throw new ArgumentNullException(nameof(growlInfo));
|
||||
if (!growlInfo.IsCustom)
|
||||
{
|
||||
growlInfo.IconKey = ResourceToken.AskGeometry;
|
||||
growlInfo.IconBrushKey = ResourceToken.AccentBrush;
|
||||
growlInfo.StaysOpen = true;
|
||||
growlInfo.ShowCloseButton = false;
|
||||
growlInfo.Type = InfoType.Ask;
|
||||
}
|
||||
Show(growlInfo);
|
||||
}
|
||||
|
||||
private void ButtonClose_OnClick(object sender, RoutedEventArgs e) => Close();
|
||||
|
||||
/// <summary>
|
||||
/// 关闭
|
||||
/// </summary>
|
||||
private void Close()
|
||||
{
|
||||
_timerClose?.Stop();
|
||||
var transform = new TranslateTransform();
|
||||
_gridMain.RenderTransform = transform;
|
||||
var animation = AnimationHelper.CreateAnimation(MaxWidth);
|
||||
animation.Completed += (s, e) =>
|
||||
{
|
||||
if (Parent is Panel panel)
|
||||
{
|
||||
panel.Children.Remove(this);
|
||||
}
|
||||
};
|
||||
transform.BeginAnimation(TranslateTransform.XProperty, animation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
public static void Clear(string token = "")
|
||||
{
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
if (PanelDic.TryGetValue(token, out var panel))
|
||||
{
|
||||
Clear(panel);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Clear(GrowlPanel);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除
|
||||
/// </summary>
|
||||
/// <param name="panel"></param>
|
||||
private static void Clear(Panel panel)
|
||||
{
|
||||
panel.Children.Clear();
|
||||
if (panel.ContextMenu != null)
|
||||
{
|
||||
panel.ContextMenu.IsOpen = false;
|
||||
panel.ContextMenu.Opacity = 1;
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonCancel_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (ActionBeforeClose?.Invoke(false) == true)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonOk_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (ActionBeforeClose?.Invoke(true) == true)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,390 +0,0 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Interactivity;
|
||||
using HandyControl.Tools;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息框
|
||||
/// </summary>
|
||||
public sealed class MessageBox : Window
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const string ElementImage = "PART_Image";
|
||||
|
||||
private const string ElementMessage = "PART_Message";
|
||||
|
||||
#endregion Constants
|
||||
|
||||
private MessageBoxResult _messageBoxResult = MessageBoxResult.Cancel;
|
||||
|
||||
private bool _showOk;
|
||||
|
||||
private bool _showCancel;
|
||||
|
||||
private bool _showYes;
|
||||
|
||||
private bool _showNo;
|
||||
|
||||
private string _message;
|
||||
|
||||
private Geometry _image;
|
||||
|
||||
private Brush _imageBrush;
|
||||
|
||||
private bool _showImage;
|
||||
|
||||
private MessageBox()
|
||||
{
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, (s, e) =>
|
||||
{
|
||||
_messageBoxResult = MessageBoxResult.OK;
|
||||
Close();
|
||||
}, (s, e) => e.CanExecute = _showOk));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Cancel, (s, e) =>
|
||||
{
|
||||
_messageBoxResult = MessageBoxResult.Cancel;
|
||||
Close();
|
||||
}, (s, e) => e.CanExecute = _showCancel));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Yes, (s, e) =>
|
||||
{
|
||||
_messageBoxResult = MessageBoxResult.Yes;
|
||||
Close();
|
||||
}, (s, e) => e.CanExecute = _showYes));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.No, (s, e) =>
|
||||
{
|
||||
_messageBoxResult = MessageBoxResult.No;
|
||||
Close();
|
||||
}, (s, e) => e.CanExecute = _showNo));
|
||||
}
|
||||
|
||||
protected override void OnSourceInitialized(EventArgs e)
|
||||
{
|
||||
base.OnSourceInitialized(e);
|
||||
|
||||
if (LogicalTreeHelper.FindLogicalNode(this, ElementMessage) is TextBlock message) message.Text = _message;
|
||||
|
||||
if (_showImage)
|
||||
{
|
||||
if (LogicalTreeHelper.FindLogicalNode(this, ElementImage) is Path image)
|
||||
{
|
||||
image.Data = _image;
|
||||
image.Fill = _imageBrush;
|
||||
image.Show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 成功
|
||||
/// </summary>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
public static MessageBoxResult Success(string messageBoxText, string caption = null)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
|
||||
SetButtonStatus(messageBox, MessageBoxButton.OK);
|
||||
messageBox._showImage = true;
|
||||
messageBox._image = ResourceHelper.GetResource<Geometry>(ResourceToken.SuccessGeometry);
|
||||
messageBox._imageBrush = ResourceHelper.GetResource<Brush>(ResourceToken.SuccessBrush);
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 消息
|
||||
/// </summary>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
public static MessageBoxResult Info(string messageBoxText, string caption = null)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
|
||||
SetButtonStatus(messageBox, MessageBoxButton.OK);
|
||||
SetImage(messageBox, MessageBoxImage.Information);
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 警告
|
||||
/// </summary>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
public static MessageBoxResult Warning(string messageBoxText, string caption = null)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
|
||||
SetButtonStatus(messageBox, MessageBoxButton.OK);
|
||||
SetImage(messageBox, MessageBoxImage.Warning);
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 错误
|
||||
/// </summary>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
public static MessageBoxResult Error(string messageBoxText, string caption = null)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
|
||||
SetButtonStatus(messageBox, MessageBoxButton.OK);
|
||||
SetImage(messageBox, MessageBoxImage.Error);
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 严重
|
||||
/// </summary>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
public static MessageBoxResult Fatal(string messageBoxText, string caption = null)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
|
||||
SetButtonStatus(messageBox, MessageBoxButton.OK);
|
||||
messageBox._showImage = true;
|
||||
messageBox._image = ResourceHelper.GetResource<Geometry>(ResourceToken.FatalGeometry);
|
||||
messageBox._imageBrush = ResourceHelper.GetResource<Brush>(ResourceToken.PrimaryTextBrush);
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 询问
|
||||
/// </summary>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
public static MessageBoxResult Ask(string messageBoxText, string caption = null)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel);
|
||||
SetButtonStatus(messageBox, MessageBoxButton.OKCancel);
|
||||
SetImage(messageBox, MessageBoxImage.Question);
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自定义信息展示
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
public static MessageBoxResult Show(MessageBoxInfo info)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(null, info.MessageBoxText, info.Caption, info.Button, MessageBoxImage.None, info.DefaultResult);
|
||||
SetButtonStatus(messageBox, info.Button);
|
||||
|
||||
if (!string.IsNullOrEmpty(info.IconKey))
|
||||
{
|
||||
messageBox._showImage = true;
|
||||
messageBox._image = ResourceHelper.GetResource<Geometry>(info.IconKey);
|
||||
messageBox._imageBrush = ResourceHelper.GetResource<Brush>(info.IconBrushKey);
|
||||
}
|
||||
|
||||
messageBox.Style = info.Style;
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 信息展示
|
||||
/// </summary>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
/// <param name="button"></param>
|
||||
/// <param name="icon"></param>
|
||||
/// <param name="defaultResult"></param>
|
||||
/// <returns></returns>
|
||||
public static MessageBoxResult Show(string messageBoxText, string caption = null,
|
||||
MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None,
|
||||
MessageBoxResult defaultResult = MessageBoxResult.None) =>
|
||||
Show(null, messageBoxText, caption, button, icon, defaultResult);
|
||||
|
||||
/// <summary>
|
||||
/// 信息展示
|
||||
/// </summary>
|
||||
/// <param name="owner"></param>
|
||||
/// <param name="messageBoxText"></param>
|
||||
/// <param name="caption"></param>
|
||||
/// <param name="button"></param>
|
||||
/// <param name="icon"></param>
|
||||
/// <param name="defaultResult"></param>
|
||||
/// <returns></returns>
|
||||
public static MessageBoxResult Show(System.Windows.Window owner, string messageBoxText, string caption = null, MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None)
|
||||
{
|
||||
MessageBox messageBox = null;
|
||||
Application.Current.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
messageBox = CreateMessageBox(owner, messageBoxText, caption, button, icon, defaultResult);
|
||||
SetButtonStatus(messageBox, button);
|
||||
SetImage(messageBox, icon);
|
||||
messageBox.ShowDialog();
|
||||
}));
|
||||
|
||||
return messageBox._messageBoxResult;
|
||||
}
|
||||
|
||||
private static MessageBox CreateMessageBox(
|
||||
System.Windows.Window owner,
|
||||
string messageBoxText,
|
||||
string caption,
|
||||
MessageBoxButton button,
|
||||
MessageBoxImage icon,
|
||||
MessageBoxResult defaultResult)
|
||||
{
|
||||
if (!IsValidMessageBoxButton(button))
|
||||
{
|
||||
throw new InvalidEnumArgumentException(nameof(button), (int)button, typeof(MessageBoxButton));
|
||||
}
|
||||
if (!IsValidMessageBoxImage(icon))
|
||||
{
|
||||
throw new InvalidEnumArgumentException(nameof(icon), (int)icon, typeof(MessageBoxImage));
|
||||
}
|
||||
if (!IsValidMessageBoxResult(defaultResult))
|
||||
{
|
||||
throw new InvalidEnumArgumentException(nameof(defaultResult), (int)defaultResult, typeof(MessageBoxResult));
|
||||
}
|
||||
|
||||
var ownerWindow = owner ?? VisualHelper.GetActiveWindow();
|
||||
var ownerIsNull = ownerWindow is null;
|
||||
|
||||
return new MessageBox
|
||||
{
|
||||
_message = messageBoxText,
|
||||
Owner = ownerWindow,
|
||||
WindowStartupLocation = ownerIsNull ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner,
|
||||
ShowTitle = true,
|
||||
Title = caption ?? string.Empty,
|
||||
Topmost = ownerIsNull,
|
||||
_messageBoxResult = defaultResult
|
||||
};
|
||||
}
|
||||
|
||||
private static void SetButtonStatus(MessageBox messageBox, MessageBoxButton messageBoxButton)
|
||||
{
|
||||
switch (messageBoxButton)
|
||||
{
|
||||
case MessageBoxButton.OK:
|
||||
messageBox._showOk = true;
|
||||
break;
|
||||
case MessageBoxButton.OKCancel:
|
||||
messageBox._showOk = true;
|
||||
messageBox._showCancel = true;
|
||||
break;
|
||||
case MessageBoxButton.YesNo:
|
||||
messageBox._showYes = true;
|
||||
messageBox._showNo = true;
|
||||
break;
|
||||
case MessageBoxButton.YesNoCancel:
|
||||
messageBox._showYes = true;
|
||||
messageBox._showNo = true;
|
||||
messageBox._showCancel = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetImage(MessageBox messageBox, MessageBoxImage messageBoxImage)
|
||||
{
|
||||
var iconKey = string.Empty;
|
||||
var iconBrushKey = string.Empty;
|
||||
|
||||
switch (messageBoxImage)
|
||||
{
|
||||
case MessageBoxImage.Error:
|
||||
iconKey = ResourceToken.ErrorGeometry;
|
||||
iconBrushKey = ResourceToken.DangerBrush;
|
||||
break;
|
||||
case MessageBoxImage.Question:
|
||||
iconKey = ResourceToken.AskGeometry;
|
||||
iconBrushKey = ResourceToken.AccentBrush;
|
||||
break;
|
||||
case MessageBoxImage.Warning:
|
||||
iconKey = ResourceToken.WarningGeometry;
|
||||
iconBrushKey = ResourceToken.WarningBrush;
|
||||
break;
|
||||
case MessageBoxImage.Information:
|
||||
iconKey = ResourceToken.InfoGeometry;
|
||||
iconBrushKey = ResourceToken.InfoBrush;
|
||||
break;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(iconKey)) return;
|
||||
messageBox._showImage = true;
|
||||
messageBox._image = ResourceHelper.GetResource<Geometry>(iconKey);
|
||||
messageBox._imageBrush = ResourceHelper.GetResource<Brush>(iconBrushKey);
|
||||
}
|
||||
|
||||
private static bool IsValidMessageBoxButton(MessageBoxButton value)
|
||||
{
|
||||
return value == MessageBoxButton.OK
|
||||
|| value == MessageBoxButton.OKCancel
|
||||
|| value == MessageBoxButton.YesNo
|
||||
|| value == MessageBoxButton.YesNoCancel;
|
||||
}
|
||||
|
||||
private static bool IsValidMessageBoxImage(MessageBoxImage value)
|
||||
{
|
||||
return value == MessageBoxImage.Asterisk
|
||||
|| value == MessageBoxImage.Error
|
||||
|| value == MessageBoxImage.Exclamation
|
||||
|| value == MessageBoxImage.Hand
|
||||
|| value == MessageBoxImage.Information
|
||||
|| value == MessageBoxImage.None
|
||||
|| value == MessageBoxImage.Question
|
||||
|| value == MessageBoxImage.Stop
|
||||
|| value == MessageBoxImage.Warning;
|
||||
}
|
||||
|
||||
private static bool IsValidMessageBoxResult(MessageBoxResult value)
|
||||
{
|
||||
return value == MessageBoxResult.Cancel
|
||||
|| value == MessageBoxResult.No
|
||||
|| value == MessageBoxResult.None
|
||||
|| value == MessageBoxResult.OK
|
||||
|| value == MessageBoxResult.Yes;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,10 +70,6 @@
|
||||
<Generator>XamlIntelliSenseFileGenerator</Generator>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Controls\Other\Growl.cs" />
|
||||
<Compile Include="Controls\Window\MessageBox.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Windows.Shell">
|
||||
<Version>3.0.1</Version>
|
||||
|
||||
Reference in new issue
Block a user