Je me demande pourquoi mon écran de démarrage ne disparaît pas après x secondes dans le code ci-dessous? En C#, je lance un thread, qui attend x secondes, puis essaie d'envoyer un close() à la fenêtre de démarrage alors qu'il est affiché sur l'écran.WPF/C# autonome minimal pour générer un écran de démarrage qui affiche pendant x secondes
// FILE: splashy.cs
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Windows;
using System.Windows.Markup;
using System.Threading;
namespace Splashy
{
class MySplash
{
Window win;
public void Show()
{
var mem = new MemoryStream(
ASCIIEncoding.UTF8.GetBytes(xmlstr));
win = (Window)XamlReader.Load(mem);
(new Thread(CloseIt)).Start();
win.Show();
}
public void CloseIt() {
Thread.Sleep(4*1000);
//MessageBox.Show("CLOSE");
win.Dispatcher.Invoke(() => win.Close());
}
static string xmlstr = @"
<Window
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
Name='WindowSplash'
Title='My Splash Window...'
WindowStyle='None'
WindowStartupLocation='CenterScreen'
Background='White'
ShowInTaskbar ='true'
Width='350' Height='130'
ResizeMode = 'NoResize' >
<Window.Resources>
<Style TargetType='{x:Type Label}'>
<Setter Property='FontFamily' Value='Consolas'/>
<Setter Property='Background' Value='Blue'/>
<Setter Property='Foreground' Value='AntiqueWhite'/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height='Auto'/>
<RowDefinition Height='*'/>
<RowDefinition Height='Auto'/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width='Auto'/>
<ColumnDefinition Width='*'/>
</Grid.ColumnDefinitions>
<Label
Grid.ColumnSpan='2'
Grid.Row='0'
Content='MyApplication 1.0'
FontWeight='Bold'
FontFamily='Consolas'
Background='AntiqueWhite'
Foreground='Black'/>
<Label
Grid.ColumnSpan='2'
Grid.Row='2'
Content=''
FontWeight='Bold'
FontFamily='Consolas'
Background='AntiqueWhite'
Foreground='Black'/>
<Label
Grid.Column='0'
Grid.Row='1'
FontFamily='Webdings' Content='Â' FontSize='80' />
<StackPanel
Grid.Row='1'
Grid.Column='1'
Background='Blue'
>
<Label Content='Programmer: John Smith'/>
<Label Content='Email: [email protected]'/>
<Label Content='Dates: 2017'/>
</StackPanel>
</Grid>
</Window>
";
} //end-class
class MyApp
{
[STAThread]
static void Main(string[] args)
{
(new MySplash()).Show();
}
} //end-class
} //end-namespace
Voici un exemple de ce que ma ligne de commande csc.exe cherche la compilation ci-dessus du code C# de système (...) à l'intérieur du mode console C++ application:
C:\WINDOWS\Microsoft.Net\Framework64\v4.0.30319\csc.exe
/out:splashy.exe
/nologo
/target:winexe
splashy.cs
/reference:"C:\WINDOWS\Microsoft.Net\Framework64\v4.0.30319\WPF\presentationframework.dll"
/reference:"C:\WINDOWS\Microsoft.Net\Framework64\v4.0.30319\WPF\windowsbase.dll"
/reference:"C:\WINDOWS\Microsoft.Net\Framework64\v4.0.30319\WPF\presentationcore.dll"
/reference:"C:\WINDOWS\Microsoft.Net\Framework64\v4.0.30319\System.Xaml.dll"
Merci! Après avoir réécrit et ajouté "Application.Run (win)" cela fonctionne. –
@Bill: en regardant vos questions, il semble que vous n'avez jamais accepté de réponse. Vous pourriez vouloir lire [Que dois-je faire quand quelqu'un répond à ma question?] (Https://stackoverflow.com/help/someone-answers) :) –