497 lines
20 KiB
C#
497 lines
20 KiB
C#
using Cowain.Bake.BLL;
|
||
using Cowain.Bake.Common.Core;
|
||
using Cowain.Bake.Common.Enums;
|
||
using Cowain.Bake.Model;
|
||
using Fluent;
|
||
using Prism.Ioc;
|
||
using Prism.Regions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Reflection;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using Unity;
|
||
using System.Linq;
|
||
using System.Windows.Media;
|
||
using Newtonsoft.Json;
|
||
using Cowain.Bake.Main.ViewModels;
|
||
|
||
|
||
namespace Cowain.Bake.Main.Views
|
||
{
|
||
/// <summary>
|
||
/// MainHeaderView.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class MainHeaderView : UserControl
|
||
{
|
||
private static Dictionary<string, RibbonGroupBox> parentGroupBoxDic = new Dictionary<string, RibbonGroupBox>();
|
||
private static IUnityContainer _unityContainer;
|
||
public static Dictionary<string, Label> toggleButtonLabelDic = new Dictionary<string, Label>();
|
||
public static Dictionary<string, Fluent.Button> ribbonButtonDic = new Dictionary<string, Fluent.Button>();
|
||
private static List<Fluent.ToggleButton> toggleButton = new List<Fluent.ToggleButton>();
|
||
private static Views.MainHeaderView MainHeaderViewForPublic;
|
||
private static List<Label> toggleBtnLabel = new List<Label>();
|
||
private static List<TMenuInfo> menuInfos;
|
||
public static Dictionary<int, StackPanel> dicStackPanel = new Dictionary<int, StackPanel>();
|
||
Fluent.DropDownButton splitButton = new Fluent.DropDownButton();
|
||
private static IRegionManager _regionManager = null;
|
||
private List<string> _theme = new List<string>();
|
||
public MainHeaderView(IUnityContainer unityContainer, IRegionManager regionManager)
|
||
{
|
||
InitializeComponent();
|
||
_regionManager = regionManager;
|
||
_unityContainer = unityContainer;
|
||
//mainRibbonForPublic = mainRibbon;
|
||
MainHeaderViewForPublic = this;
|
||
menuInfos = _unityContainer.Resolve<MenuInfoService>().GetMenuInfoList();
|
||
GenerateMenuImage();
|
||
ClearWindows();
|
||
Init();
|
||
SetTheme();
|
||
}
|
||
void SetTheme()
|
||
{
|
||
/*
|
||
“Red”, “Green”, “Blue”, “Purple”, “Orange”, “Lime”, “Emerald”, “Teal”, “Cyan”, “Cobalt”, “Indigo”, “Violet”, “Pink”, “Magenta”, “Crimson”, “Amber”, “Yellow”, “Brown”, “Olive”, “Steel”, “Mauve”, “Taupe”, “Sienna”
|
||
and these base colors: “Light”, “Dark”
|
||
*/
|
||
//var theme = ControlzEx.Theming.ThemeManager.Current.DetectTheme(Application.Current);
|
||
_theme.Add("Light.Blue");
|
||
_theme.Add("Light.Amber");
|
||
|
||
_theme.Add("Light.Magenta");
|
||
_theme.Add("Dark.Green");
|
||
_theme.Add("Light.Green");
|
||
_theme.Add("Dark.Lime");
|
||
|
||
ChangeSkin(SettingProvider.Instance.SkinType);
|
||
_unityContainer.Resolve<MainWindow>().ChangeSkin(SettingProvider.Instance.SkinType);
|
||
}
|
||
public void ChangeSkin(int i)
|
||
{
|
||
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(App.Current, _theme[i]);
|
||
}
|
||
public void ClearWindows()
|
||
{
|
||
parentGroupBoxDic.Clear();
|
||
toggleButtonLabelDic.Clear();
|
||
ribbonButtonDic.Clear();
|
||
mainRibbon.Tabs.Clear();
|
||
dicStackPanel.Clear();
|
||
}
|
||
|
||
public void Init()
|
||
{
|
||
AddMenuFatherLabelItems();
|
||
AddStackPanel();
|
||
AddMenuGroupBoxItems();
|
||
|
||
|
||
MainWindow.mainShowWindow.Title = "产量查询";
|
||
_regionManager.RequestNavigate("MainContentRegion", "ProductionsInfoView");
|
||
|
||
if (EDispatchMode.Manual == SettingProvider.Instance.DispMode)
|
||
{
|
||
ribbonButtonDic["Manual"].IsEnabled = false;
|
||
ribbonButtonDic["Auto"].IsEnabled = true;
|
||
}
|
||
else
|
||
{
|
||
ribbonButtonDic["Manual"].IsEnabled = true;
|
||
ribbonButtonDic["Auto"].IsEnabled = false;
|
||
}
|
||
mainRibbon.Tabs[0].IsSelected = true;
|
||
}
|
||
/// <summary>
|
||
/// 添加菜单标签列表
|
||
/// </summary>
|
||
/// <param name="parentId"></param>
|
||
private void AddMenuFatherLabelItems(int parentId = 0)
|
||
{
|
||
var menuParentInfos = menuInfos.Where(p => p.ParentId == parentId);
|
||
foreach (var item in menuParentInfos)
|
||
{
|
||
RibbonTabItem ribbonTabItem = new RibbonTabItem()
|
||
{
|
||
Header = item.Header,
|
||
Name = item.HeaderName
|
||
};
|
||
RibbonGroupBox ribbonGroupBox = new RibbonGroupBox();
|
||
ribbonGroupBox.Name = $"{item.Header}GroupBox";
|
||
|
||
ribbonTabItem.Groups.Add(ribbonGroupBox);
|
||
mainRibbon.Tabs.Add(ribbonTabItem);
|
||
parentGroupBoxDic.Add(item.Id.ToString(), ribbonGroupBox);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 添加工具栏功能列表
|
||
/// </summary>
|
||
private void AddMenuGroupBoxItems()
|
||
{
|
||
var roleContainNodes = _unityContainer.Resolve<UserService>().GetCurrentUserAuthority();
|
||
//var strTolist = CommonCoreHelper.Instance.StringToListConverter(roleContainNodes); && strTolist.Contains(p.HeaderName)
|
||
|
||
var menuParentIds = menuInfos.Where(p => p.ParentId == 0).ToList();
|
||
var btnContentMenuItems = menuInfos.Where(p => menuParentIds.Select(x => x.Id).Contains(p.ParentId)).OrderBy(p => p.ParentId).ThenBy(p => p.MenuIndex);
|
||
foreach (var item in btnContentMenuItems)
|
||
{
|
||
|
||
switch ((EMenuType)item.MenuType)
|
||
{
|
||
case EMenuType.Cmd:
|
||
case EMenuType.Region:
|
||
case EMenuType.ShowDialog:
|
||
FillRibbonButton(item);
|
||
break;
|
||
case EMenuType.FunctionSwitch:
|
||
DrawToggleButton(item);
|
||
break;
|
||
case EMenuType.DropDown:
|
||
DrawSplitButton(item);
|
||
break;
|
||
default:
|
||
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
#region 事件
|
||
//0.按键:弹界面
|
||
//1.按键:功能,如开始,停止
|
||
//2.开关:功能开关,如是否启用MOM
|
||
private static void RibbonButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
string btnName = "";
|
||
var type = sender.GetType();
|
||
if (type == typeof(Fluent.Button))
|
||
{
|
||
btnName = (sender as Fluent.Button).Name;
|
||
}
|
||
else
|
||
{
|
||
btnName = (sender as Fluent.ToggleButton).Name;
|
||
}
|
||
TMenuInfo menuInfo = menuInfos.FirstOrDefault(p => p.HeaderName == btnName);
|
||
if (null == menuInfo)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassWarn("查找按键信息失败!", true);
|
||
return;
|
||
}
|
||
|
||
switch ((EMenuType)menuInfo.MenuType)
|
||
{
|
||
case EMenuType.ShowDialog:
|
||
ShowDialog(menuInfo);
|
||
break;
|
||
case EMenuType.Region:
|
||
ShowWindowsRegion(menuInfo);
|
||
break;
|
||
case EMenuType.Cmd:
|
||
case EMenuType.FunctionSwitch:
|
||
ExecuteMethod(menuInfo);
|
||
break;
|
||
default:
|
||
LogHelper.Instance.Warn($"没有这个菜单[{btnName}]类型:{(int)menuInfo.MenuType}");
|
||
break;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassDebug(ex.Message);
|
||
}
|
||
}
|
||
private static void ShowDialog(TMenuInfo menuInfo)
|
||
{
|
||
TMenuInfo asmBaseName = menuInfos.FirstOrDefault(p => p.ParentId == 0 && p.Id == menuInfo.ParentId);
|
||
if (null == asmBaseName)
|
||
{
|
||
HandyControl.Controls.MessageBox.Info($"当前'{asmBaseName.Header}'目标路径不正确,请在数据库中重新配置!!");
|
||
return;
|
||
}
|
||
|
||
System.Reflection.Assembly asm = System.Reflection.Assembly.Load(asmBaseName.TargetView);// 通过类型的完整限定名和程序集获取类型
|
||
Type viewNameType = asm.GetType(menuInfo.TargetView);
|
||
if (null == viewNameType)
|
||
{
|
||
LogHelper.Instance.Info($"当前'{menuInfo.Header}'类型的完整限定名不正确,请在数据库中重新配置!", true);
|
||
return;
|
||
}
|
||
|
||
var dlg = (System.Windows.Window)_unityContainer.Resolve(viewNameType);
|
||
if (dlg.ShowDialog().Value)
|
||
{
|
||
_unityContainer.Resolve<BasicInfoViewModel>().ShowInfo();
|
||
HandyControl.Controls.MessageBox.Success("切换用户成功");
|
||
_unityContainer.Resolve<LogService>().AddLog($@"切换用户成功!", E_LogType.Operate.ToString());
|
||
}
|
||
//object typeInstance = Activator.CreateInstance(viewNameType); //不会传参数
|
||
//Window dlg = typeInstance as Window;
|
||
//dlg.ShowDialog();
|
||
}
|
||
|
||
private static void ShowWindowsRegion(TMenuInfo menuInfo)
|
||
{
|
||
MainWindow.mainShowWindow.Title = menuInfo.Header;
|
||
_regionManager.RequestNavigate("MainContentRegion", menuInfo.TargetView);
|
||
}
|
||
|
||
private static void ExecuteMethod(TMenuInfo menuInfo)
|
||
{
|
||
dynamic d = JsonConvert.DeserializeObject<dynamic>(menuInfo.JSON);
|
||
string methodName = d.CMD;
|
||
string param = d.Parameter;
|
||
Type type = Type.GetType(Cowain.Bake.Common.MyPath.HEAD_CMD);
|
||
object[] instancePara = new object[1];
|
||
instancePara[0] = _unityContainer;
|
||
object obj = System.Activator.CreateInstance(type, instancePara);
|
||
MethodInfo method = type.GetMethod(methodName, new Type[] { typeof(string) });
|
||
object[] parameters = new object[] { param };
|
||
method.Invoke(obj, parameters);
|
||
}
|
||
#endregion
|
||
|
||
#region 内部方法
|
||
|
||
|
||
private void FillRibbonButton(TMenuInfo menuInfo)
|
||
{
|
||
var ribbonGroup = parentGroupBoxDic[menuInfo.ParentId.ToString()];
|
||
Fluent.Button button = new Fluent.Button();
|
||
button.Name = menuInfo.HeaderName;
|
||
button.Header = menuInfo.Header;
|
||
button.Width = 60; //MyAppContainer.Current.Resolve<UserService>().IsHasAuthority(menuInfo.HeaderName == null ? "" : menuInfo.HeaderName);
|
||
button.IsEnabled = _unityContainer.Resolve<UserService>().IsHasAuthority(menuInfo.HeaderName == null ? "" : menuInfo.HeaderName);
|
||
string imagePath = Environment.CurrentDirectory + $@"\images\{menuInfo.Header}.png";
|
||
if (File.Exists(imagePath))
|
||
{
|
||
button.LargeIcon = imagePath; //imagePath;
|
||
}
|
||
button.Click += RibbonButton_Click;
|
||
|
||
if (ribbonGroup != null)
|
||
{
|
||
ribbonGroup.Items.Add(button);
|
||
}
|
||
|
||
if (!ribbonButtonDic.ContainsKey(menuInfo.HeaderName))
|
||
{
|
||
ribbonButtonDic.Add(menuInfo.HeaderName, button);
|
||
}
|
||
}
|
||
|
||
private void SplitButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
//if (sender is Fluent.MenuItem item)
|
||
//{
|
||
// var name = item.Tag.ToString().Replace("_", ".");
|
||
// maingrid.Background = new ImageBrush
|
||
// {
|
||
// ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri($"pack://application:,,,/Cowain.Bake.Main;component/Images/UserControls/{name}"))
|
||
// };
|
||
//}
|
||
Fluent.MenuItem currentMenu = (Fluent.MenuItem)sender;
|
||
splitButton.IsDropDownOpen = false;
|
||
|
||
for (int i = 0; i < splitButton.Items.Count; ++i)
|
||
{
|
||
Fluent.MenuItem menu = (Fluent.MenuItem)splitButton.Items.GetItemAt(i);
|
||
if (menu.Header != currentMenu.Header
|
||
&& menu.IsChecked)
|
||
{
|
||
menu.IsChecked = false;
|
||
}
|
||
}
|
||
|
||
if (currentMenu.IsChecked)
|
||
{
|
||
int value = (int)EnumHelper.GetValueByDescription<ESkin>((string)currentMenu.Header);
|
||
ChangeSkin(value);
|
||
_unityContainer.Resolve<MainWindow>().ChangeSkin(value);
|
||
SettingProvider.Instance.SkinType = value;
|
||
}
|
||
}
|
||
|
||
private Fluent.MenuItem CreateSplitButtonMenu(ESkin eskin, bool isCheck)
|
||
{
|
||
var menu = new Fluent.MenuItem
|
||
{
|
||
Header = eskin.GetDescription(),
|
||
Tag = eskin.ToString(),
|
||
IsCheckable = true,
|
||
IsChecked = isCheck, // 默认选中
|
||
};
|
||
menu.Click += SplitButton_Click;
|
||
return menu;
|
||
}
|
||
private void DrawSplitButton(TMenuInfo menuInfo)
|
||
{
|
||
splitButton.Header = menuInfo.Header;
|
||
// 设置大图标(LargeIcon)new BitmapImage(new Uri("pack://application:,,,/Images/LargeIcon.png"));
|
||
splitButton.LargeIcon = new System.Windows.Media.Imaging.BitmapImage
|
||
(new Uri(Environment.CurrentDirectory + $@"\images\{menuInfo.Header}.png"));
|
||
// 其他属性(尺寸、边距等)
|
||
splitButton.Width = 60;
|
||
//splitButton.Margin = new Thickness(1);
|
||
//splitButton.HorizontalAlignment = HorizontalAlignment.Left;
|
||
//splitButton.VerticalAlignment = VerticalAlignment.Top;
|
||
|
||
foreach (ESkin skin in System.Enum.GetValues(typeof(ESkin)))
|
||
{
|
||
splitButton.Items.Add(CreateSplitButtonMenu(skin, SettingProvider.Instance.SkinType == (int)skin));
|
||
}
|
||
|
||
var ribbonGroup = parentGroupBoxDic[menuInfo.ParentId.ToString()];
|
||
ribbonGroup.Items.Add(splitButton);
|
||
}
|
||
private static void AddStackPanel()
|
||
{
|
||
var toggles = menuInfos.Where(x => x.MenuType == (int)EMenuType.FunctionSwitch && x.State == true).ToList();
|
||
int count = toggles.Count();
|
||
var ribbonGroupBox = parentGroupBoxDic[toggles[0].ParentId.ToString()];
|
||
count = (int)Math.Ceiling((double)count / 2); //两个一组
|
||
StackPanel h = new StackPanel { Orientation = Orientation.Horizontal }; //行
|
||
ribbonGroupBox.Items.Add(h);
|
||
for (int i = 1; i <= count; i++ )
|
||
{
|
||
StackPanel v = new StackPanel { Orientation = Orientation.Vertical }; //列
|
||
h.Children.Add(v);
|
||
dicStackPanel[i] = v;
|
||
}
|
||
}
|
||
private static void DrawToggleButton(TMenuInfo item)
|
||
{
|
||
var header = item.Header;
|
||
var model = Newtonsoft.Json.JsonConvert.DeserializeObject<ParaJSON>(item.JSON);
|
||
var ribbonGroupBox = parentGroupBoxDic[item.ParentId.ToString()];
|
||
|
||
if (ribbonGroupBox == null) return;
|
||
|
||
var paraValue = _unityContainer.Resolve<MenuInfoService>().GetMenuParam(header);
|
||
var isChecked = paraValue == 1;
|
||
var labelContent = isChecked ? model.Value1 : model.Value0;
|
||
var toggleButton = CreateToggleButton(item, isChecked);
|
||
var labelRight = CreateLabel(header, labelContent);
|
||
var labelLeft = CreateLabelLeft(header, model.Text);
|
||
var canvas = new Canvas { Height = 25, Width = 200 };
|
||
canvas.Children.Add(toggleButton);
|
||
canvas.Children.Add(labelLeft);
|
||
canvas.Children.Add(labelRight);
|
||
//ribbonGroupBox.Items.Add(canvas);
|
||
toggleButtonLabelDic.Add(header, labelRight);
|
||
|
||
int index = (int)Math.Ceiling((double)item.MenuIndex / 2);
|
||
if (dicStackPanel.ContainsKey(index)) //如果MenuIndex编号乱编,会导致溢出
|
||
{
|
||
dicStackPanel[index].Children.Add(canvas);
|
||
}
|
||
|
||
}
|
||
|
||
private static Label CreateLabelLeft(string header, object content)
|
||
{
|
||
var label = new Label
|
||
{
|
||
BorderThickness = new Thickness(0),
|
||
Name = header + "HeaderLabelLeft",
|
||
Content = content
|
||
};
|
||
label.SetValue(System.Windows.Window.LeftProperty, 0d);
|
||
label.SetValue(System.Windows.Window.TopProperty, 0d);
|
||
return label;
|
||
}
|
||
|
||
private static Label CreateLabel(string header, object content)
|
||
{
|
||
var label = new Label
|
||
{
|
||
BorderThickness = new Thickness(0),
|
||
Name = header + "HeaderLabel",
|
||
Content = content
|
||
};
|
||
label.SetValue(System.Windows.Window.LeftProperty, 120d);
|
||
label.SetValue(System.Windows.Window.TopProperty, 0d);
|
||
return label;
|
||
}
|
||
|
||
private static ToggleButton CreateToggleButton(TMenuInfo item, bool isChecked)
|
||
{
|
||
var toggleButton = new ToggleButton
|
||
{
|
||
Name = item.HeaderName,
|
||
Content = item.Header,
|
||
Header = item.Header,
|
||
Width = 50,
|
||
Style = MainHeaderViewForPublic.FindResource("ToggleButtonStyle") as Style,
|
||
IsChecked = isChecked
|
||
};
|
||
toggleButton.Click += RibbonButton_Click;
|
||
toggleButton.SetValue(System.Windows.Window.LeftProperty, 69d);
|
||
toggleButton.SetValue(System.Windows.Window.TopProperty, 5d);
|
||
return toggleButton;
|
||
}
|
||
|
||
private void ChangeModeColor(Fluent.ComboBox comboBox)
|
||
{
|
||
string comboBoxColor = Colors.Black.ToString();
|
||
switch ((int)comboBox.SelectedValue)
|
||
{
|
||
case (int)EMOMMode.OnLine:
|
||
comboBoxColor = Colors.Green.ToString();
|
||
break;
|
||
|
||
case (int)EMOMMode.OffLine:
|
||
comboBoxColor = Colors.Red.ToString();
|
||
break;
|
||
}
|
||
comboBox.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(comboBoxColor));
|
||
}
|
||
#endregion
|
||
|
||
#region
|
||
private void GenerateMenuImage()//获取菜单图标
|
||
{
|
||
var table = _unityContainer.Resolve<MenuInfoService>().GetImage();
|
||
byte[] imageByte = null;
|
||
string path = Environment.CurrentDirectory + "/images";
|
||
if (!Directory.Exists(path))
|
||
{
|
||
Directory.CreateDirectory(path);
|
||
}
|
||
|
||
foreach (DataRow item in table.Rows)
|
||
{
|
||
imageByte = Newtonsoft.Json.JsonConvert.DeserializeObject<byte[]>(item["MenuImage"].ToString());
|
||
CreateFile(imageByte, path + "/" + item["Header"] + ".png");
|
||
}
|
||
}
|
||
|
||
private static void CreateFile(byte[] fileBuffer, string newFilePath)
|
||
{
|
||
if (File.Exists(newFilePath))
|
||
{
|
||
File.Delete(newFilePath);
|
||
}
|
||
FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
|
||
BinaryWriter bw = new BinaryWriter(fs);
|
||
bw.Write(fileBuffer, 0, fileBuffer.Length); //用文件流生成一个文件
|
||
bw.Close();
|
||
fs.Close();
|
||
}
|
||
#endregion
|
||
}
|
||
public class ParaJSON
|
||
{
|
||
public string CMD { get; set; }
|
||
public string Parameter { get; set; }
|
||
public string Value0 { get; set; }
|
||
public string Value1 { get; set; }
|
||
public string Text { get; set; }
|
||
}
|
||
}
|