WPF in C# interactive (csi.exe)

602 views Asked by At

I've started to play with c# interactive. In Visual Studio I was able to create some window with following code:

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

However due to this error using csi.exe I had to do something like this to see a window (I've run executable from net.compiler nuget package, tools directory):

#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();

The window is displayed while the thread is running but I'm not sure how I could achieve behavior similar to this inside VS C# interactive view.

1

There are 1 answers

4
Maciek Świszczowski On

[Edited]

Hi Paweł,

Here is an answer (tested in csi this time), based on: 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();