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

  • Tutorial
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.

publicclassMyWindow : 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 A components in
more detail.


[STAThread]publicstaticvoidMain(string[]args){}

Now create our window:

[STAThread]publicstaticvoidMain(string[]args)
{
    var win = new MyWindow { Width = 350, Height = 350};
    vargrid = newGrid();
    vartext = newTextBox {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]publicstaticvoidMain(string[]args)
        {
            var app = new Application();
            var win = new MyWindow { Width = 350, Height = 350};
            vargrid = newGrid();
            vartext = newTextBox {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.
<Windowxmlns="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><LabelContent="Label" /></Grid></Window>


Now load the data from the file.
XmlTextReaderr = newXmlTextReader("MyWin.xaml");
varwin = XamlReader.Load(r) asWindow;


And in this embodiment, the final Main looks
So.
[STAThread]
        publicstaticvoidMain(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 .

Also popular now: