alielie
2026-04-23 ef52fd6fd07508883dce4eb002acda59f0169e59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using HMI.Base;
using HMI.Core.Entity;
using HMI.Core.Setting;
using HMI.Core.Util;
using HMI.UI.Util;
using HMI.Util.Alarm;
using Panuon.WPF.UI;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
 
namespace WPFProject
{
    /// <summary>
    /// LoadingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoadingWindow : WindowX
    {
 
        private string message = "";
 
        public string Message
        {
            get { return message; }
            set { Set(ref message, value); }
        }
 
        private bool HasErr = false;
 
        private bool showConsole = false;
        public bool ShowConsole
        {
            get => showConsole;
            set
            {
                Set(ref showConsole, value);
                NotifyOfPropertyChange("ConsoleVisiable");
                NotifyOfPropertyChange("BarVisiable");
            }
        }
 
        public Visibility ConsoleVisiable
        {
            get => showConsole ? Visibility.Visible : Visibility.Collapsed;
        }
 
        public Visibility BarVisiable
        {
            get => showConsole ? Visibility.Collapsed : Visibility.Visible;
        }
 
        public LoadingWindow()
        {
            DataContext = this;
            InitializeComponent();
            logo.Source = new BitmapImage(new Uri(Path.Combine(PathHelper.IconFolder,
                     SystemSetting.Instance.LoginLogoPath ?? "LoginLogo.png")));
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
 
        }
        private void Loader_LoadMessageAppend(object sender, LoadMsg msg)
        {
            Dispatcher.BeginInvoke(() =>
            {
                switch (msg.Type)
                {
                    case LoadMsgType.INIT:
                        total = msg.NumData;
                        Add(msg.Message, msg.Result);
                        break;
                    case LoadMsgType.LOADING:
                        HasErr = HasErr || msg.Result == false;
                        if (msg.Result != null)
                        {
                            processBar.Value = ++finished / total * 100;
                        }
                        Add(msg.Message, msg.Result);
                        break;
                    case LoadMsgType.FINISHED:
                        LoadFinished();
                        break;
                    default:
                        break;
                }
 
            });
        }
 
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Closed -= ShutDownWhen_Closed;
            window.Show();
            this.Close();
        }
 
        private PanuonWindow window;
        private double total;
        private double finished;
 
        private void WindowX_ContentRendered(object sender, System.EventArgs e)
        {
            window = new PanuonWindow();
            PluginLoader loader = PluginLoader.GetLoader();
            loader.LoadMessageAppend += Loader_LoadMessageAppend;
            AlarmUtil.LoadMessageAppend += Loader_LoadMessageAppend;
            Task.Run(() =>
            {
                AlarmUtil.Init();
                loader.LoadMenuAndPlugin(window, Dispatcher);
                //
            });
        }
 
        private void LoadFinished()
        {
            if (!HasErr)
            {
                Button_Click(null, null);
            }
            else
            {
                WindowXCaption.SetButtons(this, CaptionButtons.Close);
                ShowConsole = true;
                BtnNext.Visibility = Visibility.Visible;
                MsgBoxHelper.Error(this, this.lang("App.LoadPluginError"));
                this.Closed += ShutDownWhen_Closed; ;
            }
        }
 
        private void ShutDownWhen_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }
 
        public void Add(string text, bool? result)
        {
            Message = text;
            string time = string.Format("{0:G}", DateTime.Now);
            Run run = new Run();
            string type = result != false ? "Info" : "Warn";
            run.Text = $"{time} {type} {text}";
            run.Foreground = result == null ? Brushes.Black : (result == true ? Brushes.Green : Brushes.Red);
 
            Paragraph paragraph = new Paragraph(run)
            {
                LineHeight = 1
            };
            //也可以插入到最后面
            Console.Document.Blocks.InsertAfter(Console.Document.Blocks.LastBlock, paragraph);
 
            //插入到最前面
            //rtxtLog.Document.Blocks.Add(paragraph);
 
            // 滚动至最后行
            //rtxtLog.ScrollToEnd();
            //Console.ScrollToHome();
            Console.ScrollToEnd();
            // 删除
            //if (Console.Document.Blocks.Count > 10000)
            //{
            //    for (int i = 1000; i < Console.Document.Blocks.Count; i++)
            //    {
            //        _ = Console.Document.Blocks.Remove(Console.Document.Blocks.FirstBlock);
            //    }
            //}
        }
    }
}