Une fois, j'avais besoin d'ouvrir une fenĂȘtre Ă partir d'une application console. Je voulais le faire en utilisant wpf, mais il y avait peu d'informations dispersĂ©es sur le rĂ©seau, j'ai donc dĂ©cidĂ© d'organiser et de soumettre ce petit tutoriel.
Créez une application console standard sur le framework .net.

Vous devez maintenant ajouter les dépendances: WindowsBase, PresentationCore, PresentationFramework.

Ajoutez la classe de notre fenĂȘtre, en l'hĂ©ritant des fenĂȘtres standard de Windows.
public class MyWindow : Window{}
Ajoutez l'attribut [STAThread] à la méthode principale
PourquoiSTAThreadAttribute est essentiellement une condition préalable à la messagerie avec un serveur de messages Windows avec des composants COM
Et plus en détail. [STAThread] public static void Main(string[] args){}
CrĂ©ez maintenant notre fenĂȘtre:
[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; }
Si nous appelons maintenant la mĂ©thode Show () sur la fenĂȘtre, elle s'effondrera immĂ©diatement, et puisque nous aimerions la regarder tout le temps, nous devons pousser cette fenĂȘtre dans un conteneur qui prend en charge le cycle de vie entier.
app.MainWindow = win; app.MainWindow.Show(); app.Run();
Nous avons affichĂ© une fenĂȘtre, et cela semble plutĂŽt bien, mais la fermer Ă partir du code n'est pas si facile: la mĂ©thode Run () est une boucle sans fin, et l'application ne peut ĂȘtre arrĂȘtĂ©e qu'Ă partir du mĂȘme thread oĂč elle est appelĂ©e. Sortie:
Task.Run(async () => { await Task.Delay(1000); app.Dispatcher.Invoke((Action) delegate { app.Shutdown(); }); }); ;
Ensuite, toute la méthode ressemble
donc. [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(); }
et voici la source Une solution agrĂ©able n'est pas de transformer notre fenĂȘtre en code, mais de passer au xaml plus familier.
Pour ce faire, ajoutez la dépendance System.Xml.
Et faites un document xaml.
<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>
Maintenant, chargez les données du fichier.
XmlTextReader r = new XmlTextReader("MyWin.xaml"); var win = XamlReader.Load(r) as Window;
Et dans ce cas, la finale ressemble principale
donc. [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
Merci au # chat dans
tg et Ă l'utilisateur
Yuri .