gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:28 27 4
gpt4 key购买 nike

我已经开始使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com