WinMain.xaml.cs 3.9 KB
using HHECS.Bll;
using HHECS.Controls;
using HHECS.Model;
using HHECS.View.Win;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
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.Imaging;
using System.Windows.Shapes;

namespace HHECS.View
{
    /// <summary>
    /// Main.xaml 的交互逻辑
    /// </summary>
    public partial class WinMain : BaseWindow
    {
        #region 属性

        /// <summary>
        /// 时钟同步锁
        /// </summary>
        private object locker = new object();

        /// <summary>
        /// 子窗体
        /// </summary>
        public Dictionary<string, Window> ChildrenWin { get; set; } = new Dictionary<string, Window>();

        /// <summary>
        /// 主控时钟
        /// </summary>
        private System.Timers.Timer timer = new System.Timers.Timer();

        /// <summary>
        /// 结束时钟标志
        /// </summary>
        private bool stop =  false;

        /// <summary>
        /// 用于创建设备PLC
        /// </summary>
        public List<Plc> PLCs { get; set; }

        #endregion

        public WinMain()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
            this.Lab1.Content = $"欢迎:{AppSession.User.UserName}   {DateTime.Now.ToLocalTime()}";

            this.GridMain.Children.Add(new StockerMonitor(32, 4, false, 900, 150));
        }

        #region 时钟逻辑

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (locker)
            {
                #region 是否结束处理

                if (stop)
                {

                }

                #endregion
            }
        }

        #endregion

        #region 窗体事件

        private void InitTimer()
        {
            timer.Interval = AppSession.Interval; //1秒触发一次
            timer.Elapsed += Timer_Elapsed; ;
            timer.AutoReset = true;//每到指定时间Elapsed事件是到时间就触发
            timer.Enabled = false; //指示 Timer 是否应引发 Elapsed 事件。
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            AppSession.Bll.Combine(AppSession.MenuOperations.Where(t => t.ParentId == null).ToList(), AppSession.Bll.GetAllMenuOperation().Data.Where(t => t.MenuType == "catalog" || t.MenuType == "menu").ToList());
            MenuMain.ItemsSource = AppSession.MenuOperations.Where(t => t.ParentId == null).OrderBy(t=>t.OrderNum).ToList();

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MenuMain_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                MenuOperation menu = (MenuOperation)((MenuItem)e.OriginalSource).Header;
                var win = ChildrenWin.FirstOrDefault(t => t.Key == menu.Url).Value;
                if (String.IsNullOrWhiteSpace(menu?.Url) || menu.Url == "#")
                {
                    return;
                }
                if (win == null)
                {
                    win = (Window)Activator.CreateInstance(null, menu.Url).Unwrap();
                    ChildrenWin.Add(menu.Url, win);
                }
                win.Show();
                if (win.WindowState == WindowState.Minimized)
                    win.WindowState = WindowState.Normal;
                win.Activate();
            }
            catch (Exception ex)
            {
                LogExecute.WriteExceptionLog("打开菜单", ex);
                MessageBox.Show("打开菜单出现异常!");
            }
        } 
        #endregion
    }
}