Prev | Current Page 158 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"


The Application Life Cycle
In WPF, applications go through a straightforward life cycle. Shortly after your application
begins, the application object is created. As your application runs, various application events
fire, which you may choose to monitor. Finally, when the application object is released, your
application ends.
Creating an Application Object
If you want to do all the heavy lifting yourself, you can create an instance of the WPF Application
class by hand. To do so, you must configure your application to start by running a shared
Main() method. (This is the same technique you used in Chapter 2 when loading XAML
dynamically.) The following Program class shows an example??”it includes a Main() method
that creates a window named Window1 and fires up a new application:
Public Class Startup
Inherits Application
Shared Sub Main()
' Create the application.
Dim app As New Application()
59
C H A P T E R 3
' Create the main window.
Dim win As New Window1()
' Launch the application and show the main window.
app.Run(win)
End Sub
End Class
When you pass a window to the Application.Run() method, that window is set as the main
window and exposed to your entire application through the Application.MainWindow property.
The Run() method then fires the Application.Startup event and shows the main window.


Pages:
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170