Procédure : obtenir et définir la fenêtre d’application principale
Cet exemple montre comment obtenir et définir la fenêtre principale de l’application.
Exemple
La première Window instanciée dans une application WPF (Windows Presentation Foundation) est automatiquement définie par Application comme fenêtre principale de l’application. Le premier Window à être instancié sera très probablement la fenêtre spécifiée comme Uniform Resource Identifier (URI) de démarrage (voir StartupUri).
Le premier Window peut également être instancié à l’aide du code. Un exemple montre comment ouvrir une fenêtre au démarrage de l’application, comme suit :
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
MainWindow window = new MainWindow();
window.Show();
}
}
Partial Public Class App
Inherits Application
Private Sub App_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
Dim window As New MainWindow()
window.Show()
End Sub
End Class
Parfois, le premier Window instancié n’est pas réellement la fenêtre d’application principale, par exemple un écran de démarrage. Dans ce cas, vous pouvez spécifier la fenêtre d’application principale à l’aide du balisage, comme suit :
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="StartupWindow.xaml"
>
<Application.MainWindow>
<NavigationWindow Source="MainPage.xaml" Visibility="Visible"></NavigationWindow>
</Application.MainWindow>
</Application>
Que la fenêtre principale soit spécifiée automatiquement ou manuellement, vous pouvez obtenir la fenêtre principale à partir de MainWindow à l’aide du code suivant, comme suit :
// Get the main window
Window mainWindow = this.MainWindow;
' Get the main window
Dim mainWindow As Window = Me.MainWindow
.NET Desktop feedback