如何将ironruby的输入重定向到文本框(WinForms和Silverlight 4)

发布于 2024-09-25 01:02:07 字数 993 浏览 3 评论 0原文

我正在 silverlight 4 和 WinForms (net4) 中构建 IronRuby 控制台。我可以毫无问题地重定向输出:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a='123'\nputs a", SourceCodeKind.Statements);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

现在,我也想重定向输入,但总是从脚本中得到“nil”:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
msInput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyRuntime.IO.SetInput(msInput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a=gets\nputs a", SourceCodeKind.Statements);
byte[] byteArray = Encoding.UTF8.GetBytes("123");
msInput.Write(byteArray, 0, byteArray.Length);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

我找不到任何重定向输入的示例,您可以发送一个示例吗?谢谢。

I'm building an IronRuby Console in silverlight 4 and WinForms (net4). I can redirect the output without problems:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a='123'\nputs a", SourceCodeKind.Statements);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

Now, I want to redirect the input also, but always getting a 'nil' from the script:

MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
msInput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyRuntime.IO.SetInput(msInput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a=gets\nputs a", SourceCodeKind.Statements);
byte[] byteArray = Encoding.UTF8.GetBytes("123");
msInput.Write(byteArray, 0, byteArray.Length);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);

I cannot find any samples of redirecting the input, can you please send an example? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-10-02 01:02:07

我没有任何立即可用的示例代码,但您需要实现流,而不是使用 MemoryStream。当读取流时,您需要将文本框的“内容”发送到流。您需要某种机制来确定何时发送内容 - 例如,何时用户按下回车键。您可能还需要设置一个线程来阻止读取,并可能使用 AutoResetEvent 进行阻止,直到文本框发出信号表明输入已完成。

I don't have any sample code immediately available but instead of using a MemoryStream you need to implement the stream. When reads on the stream occur you need to send the "contents" of the text box to the stream. You'll need some mechanism for determining when you send the contents - e.g. when the user hits return. You'll also probably need to setup a thread for blocking for the reads and probably use an AutoResetEvent to block until the text box signals that the input is complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文