Files

170 lines
6.7 KiB
C#

using AllBase.Helper;
using NaiBa.Util.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Work.BaseForms;
namespace _29WxOrderWpf.BaseWindows
{
/// <summary>
/// BaseMsgWindow.xaml 的交互逻辑
/// </summary>
public partial class BaseMsgWindow : BaseWindow
{
public BaseMsgWindow()
{
InitializeComponent();
this.DataContext = this;
}
private static BaseMsgWindow CreateMsgForm(string msg, string caption, MsgWindowLevel wl)
{
BaseMsgWindow msgBox = new BaseMsgWindow { lblTitle = { Content = caption }, tbkMsg = { Text = msg } };
if (wl.Equals(MsgWindowLevel.LARGE))
{
msgBox.Width = (int)MsgWindowLevel.LARGE;
msgBox.Height = 500;
}
msgBox.btnOk.Margin = new Thickness(msgBox.btnCancel.Margin.Left, msgBox.btnCancel.Margin.Top, msgBox.btnCancel.Margin.Right, msgBox.btnCancel.Margin.Bottom); // 把确认按钮移到取消按钮上面
msgBox.btnCancel.Visibility = Visibility.Collapsed;
return msgBox;
}
public static BaseMsgWindow ShowInfo(string msg, string caption = "", Window window = null, MsgWindowLevel wl = MsgWindowLevel.MEDIUM)
{
string title = string.IsNullOrEmpty(caption) ? "系统提示" : caption;
BaseMsgWindow msgBox = null;
Window owner = window ?? MainWindow.Instance;
//owner.Dispatcher.Invoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
msgBox = CreateMsgForm(msg, title, wl);
msgBox.imgTip.Source = Application.Current.Resources["imgBaseMsgInfo"] as ImageSource;
//msgBox.Owner = owner;
msgBox.ShowDialog();
});
return msgBox;
}
public static BaseMsgWindow ShowError(string msg, string caption = "", Window window = null, MsgWindowLevel wl = MsgWindowLevel.MEDIUM)
{
string title = string.IsNullOrEmpty(caption) ? "系统错误" : caption;
BaseMsgWindow msgBox = null;
Window owner = window ?? MainWindow.Instance;
//owner.Dispatcher.Invoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
msgBox = CreateMsgForm(msg, title, wl);
msgBox.imgTip.Source = Application.Current.Resources["imgBaseMsgErr"] as ImageSource;
//msgBox.Owner = owner;
msgBox.ShowDialog();
});
return msgBox;
}
public static BaseMsgWindow ShowConfirm(string msg, string caption = "", Window window = null, MsgWindowLevel fl = MsgWindowLevel.MEDIUM, int? boxHeight = null)
{
string title = string.IsNullOrEmpty(caption) ? "确认系统提示" : caption;
BaseMsgWindow msgBox = null;
Window owner = window ?? MainWindow.Instance;
//owner.Dispatcher.Invoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
msgBox = new BaseMsgWindow
{
lblTitle = { Content = title },
tbkMsg = { Text = msg },
imgTip = { Source = Application.Current.Resources["imgBaseMsgQue"] as ImageSource },
};
if (fl.Equals(MsgWindowLevel.LARGE))
{
msgBox.Width = (int)MsgWindowLevel.LARGE;
if (boxHeight == null)
boxHeight = 500;
msgBox.Height = boxHeight.ParseToInt();
}
//msgBox.Owner = owner;
msgBox.ShowDialog();
});
return msgBox;
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
public static void ShowAnimateMessage(string message, string type = "Msg", int DurationSeconds = 2)
{
MainWindow.Instance.Dispatcher.Invoke(new Action(() =>
{
if (type.ToLower() == "msg")
{
MainWindow.Instance.borderMessage.BorderBrush = Application.Current.Resources["pageColorBrush"] as SolidColorBrush;
MainWindow.Instance.tbMessage.Foreground = Application.Current.Resources["pageColorBrush"] as SolidColorBrush;
}
if (type.ToLower() == "err" || type.ToLower() == "error")
{
MainWindow.Instance.borderMessage.BorderBrush = Application.Current.Resources["PageWarningBrush"] as SolidColorBrush;
MainWindow.Instance.tbMessage.Foreground = Application.Current.Resources["PageWarningBrush"] as SolidColorBrush;
}
MainWindow.Instance.tbMessage.Text = message;
MainWindow.Instance.borderMessage.Visibility = Visibility.Visible;
// 创建Storyboard并添加动画
Storyboard storyboard = new Storyboard();
DoubleAnimation fadeInAnimation1 = new DoubleAnimation
{
From = 0, // 开始时完全透明
To = 1, // 结束时完全不透明
Duration = new Duration(TimeSpan.FromSeconds(0.2)) // 持续时间2秒
};
Storyboard.SetTarget(fadeInAnimation1, MainWindow.Instance.borderMessage);
Storyboard.SetTargetProperty(fadeInAnimation1, new PropertyPath(OpacityProperty));
storyboard.Children.Add(fadeInAnimation1);
DoubleAnimation fadeInAnimation2 = new DoubleAnimation
{
BeginTime = new TimeSpan(0, 0, DurationSeconds),
From = 1,
To = 0,
Duration = new Duration(TimeSpan.FromSeconds(0.2)) // 持续时间2秒
};
Storyboard.SetTarget(fadeInAnimation2, MainWindow.Instance.borderMessage);
Storyboard.SetTargetProperty(fadeInAnimation2, new PropertyPath(OpacityProperty));
storyboard.Children.Add(fadeInAnimation2);
// 开始动画
storyboard.Begin();
}));
}
}
public enum MsgWindowLevel
{
SMALL,
MEDIUM,
LARGE = 500
}
}