using HMI.Base; using HMI.UI.Util; using Panuon.WPF.UI; using System; using System.Threading.Tasks; using System.Windows; using System.Windows.Documents; using System.Windows.Media; namespace WPFProject { /// /// LoadingWindow.xaml 的交互逻辑 /// 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(); } private void Window_Loaded(object sender, RoutedEventArgs e) { } private void Loader_LoadMessageAppend(object sender, LoadMsg msg) { switch (msg.Type) { case LoadMsgType.INIT: total = msg.NumData; Add(msg.Message, null); break; case LoadMsgType.LOADING: 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) { PanuonWindow window = new PanuonWindow(); window.Show(); this.Close(); } private double total; private double finished; private void WindowX_ContentRendered(object sender, System.EventArgs e) { PluginLoader loader = PluginLoader.GetLoader(); loader.LoadMessageAppend += Loader_LoadMessageAppend; Task.Run(() => { loader.LoadMenuAndPlugin(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, "加载出错,请确认"); } } 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); // } //} } } }