c# - C# 中的 WPF 交互式 (csi.exe)

标签 c# wpf c#-interactive csi

我已经开始使用 c# interactive 了。在 Visual Studio 中,我能够使用以下代码创建一些窗口:

#r "PresentationFramework"
using System.Windows;
var w = new Window();
w.Show();

但是由于this error使用 csi.exe 我必须做这样的事情才能看到一个窗口(我已经从 net.compiler nuget 包,工具目录运行可执行文件):

#r "PresentationFramework"
using System.Windows;
using System.Threading;
var t = new Thread(()=>{var w = new Window(); w.Show(); Thread.Sleep(2000);});
t.SetApartmentState(ApartmentState.STA);
t.Start();

窗口在线程运行时显示,但我不确定如何在 VS C# 交互式 View 中实现与此类似的行为。

最佳答案

[已编辑]

嗨,帕维尔,

这是一个答案(这次在 csi 中测试),基于:Launching a WPF Window in a Separate Thread

#r "PresentationFramework"
#r "Microsoft.VisualStudio.Threading"

using System.Windows.Controls;
using System.Windows;
using System.Threading;

var newWindowThread = new Thread(new ThreadStart(() =>
{
    var tempWindow = new Window();
    var t = new TextBox() {Text = "test"};
    tempWindow.Content = t;
    tempWindow.Show();
    System.Windows.Threading.Dispatcher.Run();
}));


newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();

关于c# - C# 中的 WPF 交互式 (csi.exe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50437177/

相关文章:

c# - 知道 "Datacontext"何时在 UserControl 的代码隐藏中更改

c# - pig 拉丁语翻译。 C#作业

c# - 如何获取元数据自定义属性?

c# - 等待和异步阻塞 UI

wpf - 如何使用 Prism MVVM 连接 Awesomium WebControl 事件

c# - 从 linq 查询突出显示 ListView 中的文本

c# - 在 WPF 中轻松地将通知传播到 UI

c# - VS2015 C# interactive : error CS7069: Reference to type 'Object' claims it is defined in 'System.Runtime' , 但找不到