使用MVVM实时读取cmd输出错误数据

我正在编写一个程序,该程序读取cmd脚本输出,并在日志(查看模型)中显示结果。由于脚本运行了很长时间,因此我希望能够在每行写完后看到输出。现在,我只能在过程结束时才能看到输出。我不知道该怎么做?Wpf-MVVM。

StepViewModel


        public StepsViewModel()
        {
            RunSteps = new RelayCommand(OnRunSteps);
            Steps = new ObservableCollection<Step>();
            AddSteps();
        }

        private void OnRunSteps(object obj)
        {
            IsEnabled = false;
            foreach (var step in Steps)
            {
                if (step.IsChecked)
                    StepsManager.Instance.RunStep(step);
            }
            IsEnabled = true;
        }  

StepsManager

        public void RunStep(Step step)
        {
            switch (step.Name)
            {
                case "MyStep":
                    MyStep(0); // 0 is the index of the step
                    break;
            }
        } 

        private void MyStep(int stepIndex)
        {
            VMLocator.LogsViewModel.Logs.Add(new Log("MyStep",false)); // fale -> green colore,red -> red color
;
            string command = @"mycmd";

            ExecuteCmd(command);
        }

        private void ExecuteCmd(string command)
        {
            System.Diagnostics.ProcessStartInfo procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd","/c " + command)
                {
                    RedirectStandardError = true,RedirectStandardOutput = true,UseShellExecute = false,CreateNoWindow = true
                };

            // The following commands are needed to redirect the standard output.
            // This means that it will be redirected to the Process.StandardOutput StreamReader.
            // Do not create the black window.
            System.Diagnostics.Process
                proc = new System.Diagnostics.Process
                {
                    StartInfo = procStartInfo
                }; // Now we create a process,assign its ProcessStartInfo and start it
            proc.Start();

            List<string> outputData = new List<string>();
            List<string> errordata = new List<string>();

            proc.OutputDataReceived += (s,e) =>
            {
                lock (outputData)
                {
                    if (!string.IsnullOrEmpty(e.Data))
                     {
                       outputData.Add(e.Data);
                       UpdateLog(e.Data,stepIndex);
                     }                      
                }
            };

            proc.ErrordataReceived += (s,e) =>
            {
                lock (errordata)
                {
                    if (!string.IsnullOrEmpty(e.Data))
                    {
                        errordata.Add(e.Data);
                        UpdateLog(e.Data,stepIndex);
                    }
                }
            };

            proc.BeginOutputReadLine();
            proc.BeginErrorReadLine();

            proc.WaitForExit();

        }

        private void UpdateLog(string log)
        {
            if(few checks for log)
                 VMLocator.LogsViewModel.Logs.Add(new Log(log,red -> red color
        }

LogViewModel

        public ObservableCollection<Log> Logs { get; set; }
        #endregion

        public LogsViewModel()
        {
            Logs = new RelayCommand.AsyncObservableCollection<Log>();
        }

日志

        public string Message { get; set; }
        public string Color { get; set; }

        public Log(string message,bool error)
        {
            Message = message;
            Color = error ? "Red" : "Green";
        }
        public Log(string message,string color)
        {
            Message = message;
            Color = color;
        }

veralan329 回答:使用MVVM实时读取cmd输出错误数据

这种任务的技巧是确保WPF在数据更改时获得更改通知事件。

如果您将outputDataerrorData设为可观察的集合,则它们将在更改时通知WPF。

本文链接:https://www.f2er.com/3122981.html

大家都在问