📜 ⬆️ ⬇️

If the windows open, it means - someone needs it.

Once I needed to open a window from a console application. I wanted to do this using wpf, but the information scattered across the network turned out to be small, so I decided to somehow systematize and present this little tutorial.

Let's create a regular console application on the .net framework.



Now you need to add dependencies: WindowsBase, PresentationCore, PresentationFramework.



Add the class of our window, inheriting it from the standard windows of Windows.

public class MyWindow : Window{} 

Add the [STAThread] attribute to the main method

What for
STAThreadAttribute is essentially a requirement for messaging with a Windows message server with COM components.
And more.


 [STAThread] public static void Main(string[] args){} 

Now create our window:

  [STAThread] public static void Main(string[] args) { var win = new MyWindow { Width = 350, Height = 350}; var grid = new Grid(); var text = new TextBox {Text = "my text"}; grid.Children.Add(text); win.Content = grid; } 

If we now call the Show () method on the window, it will immediately collapse, and since we would like to look at it all the time, we need to push this window into a container that supports the entire life cycle.

 app.MainWindow = win; app.MainWindow.Show(); app.Run(); 

We have displayed the window, and it feels rather well, but closing it from the code simply won't work: the Run () method is an infinite loop of itself, and Application can only be stopped from the same thread where it was called. Output:

 Task.Run(async () => { await Task.Delay(1000); app.Dispatcher.Invoke((Action) delegate { app.Shutdown(); }); }); ; 

Then the whole method looks like
So.
 [STAThread] public static void Main(string[] args) { var app = new Application(); var win = new MyWindow { Width = 350, Height = 350}; var grid = new Grid(); var text = new TextBox {Text = "my text"}; grid.Children.Add(text); win.Content = grid; app.MainWindow = win; app.MainWindow.Show(); Task.Run(async () => { await Task.Delay(1000); app.Dispatcher.Invoke((Action) delegate { app.Shutdown(); }); }); app.Run(); } 

here is the source


A pleasant solution would be not to make our window from the code, but to switch to a more familiar xaml.

To do this, add the dependency System.Xml.
And compile the xaml document.
 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ConsoleApplication1" mc:Ignorable="d" Title="MyWindow" Height="450" Width="800"> <Grid> <Label Content="Label" /> </Grid> </Window> 


Now load the data from the file.
 XmlTextReader r = new XmlTextReader("MyWin.xaml"); var win = XamlReader.Load(r) as Window; 


And in this embodiment, the final Main looks
So.
 [STAThread] public static void Main(string[] args) { var app = new Application(); XmlTextReader r = new XmlTextReader("MyWin.xaml"); var win = XamlReader.Load(r) as Window; app.MainWindow = win; app.MainWindow.Show(); Task.Run(async () => { await Task.Delay(1000); app.Dispatcher.Invoke((Action) delegate { app.Shutdown(); }); }); app.Run(); } 



PS
Thanks to # chat in tg and user Yuri .

Source: https://habr.com/ru/post/437642/