永远不会触发Powershell输出的WPF事件处理程序

时间:2018-03-29 17:43:31

标签: c# wpf powershell data-binding event-handling

我正在处理通过PowerShellInstance执行MSTest的WPF应用程序。测试运行但从不触发输出事件处理程序。以下是我的ViewModel中的代码:

 private void RunMSTest(string parameters)
    {

        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            string args = parameters;
            PowerShellInstance.AddCommand("Start-Process");
            PowerShellInstance.AddParameter("FilePath", "mstest");
            PowerShellInstance.AddArgument(args);

            PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();

            outputCollection.DataAdded += outputCollection_DataAdded;
            PowerShellInstance.Streams.Error.DataAdded += Error_DataAdded;

            IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);

            while (result.IsCompleted == false)
            {
                Console.WriteLine("[While] Running ...");
                Model.Output = outputCollection;
            }

            Console.WriteLine("Test completed! "+ PowerShellInstance.InvocationStateInfo.State);
            foreach(PSObject outputItem in outputCollection)
            {
                Console.WriteLine("Output Item" + outputItem);
            }
            PowerShellInstance.EndInvoke(result);
        }

    }

我的事件处理程序在同一个文件中:

private void outputCollection_DataAdded(object sender, DataAddedEventArgs e)
    {
        Model.Output = sender.ToString();
        Console.WriteLine("** An Item was added to the Output stream.");
    }

在此事件处理程序上设置断点时,永远不会触发它。

0 个答案:

没有答案