[5][3]Application object
- It is an interface between your application and the system
- It is accessible from within all WPF .NET projects and provides an interface between your application and the system
- It is the entry point of a WPF application
- Threads in a WPF application must run in a singlethreaded apartment (STA)
- Main must be marked with an STAThread attribute.
- In the WPF sense, an application is a singleton object that provides services for UI components and UI programmers in the creation and execution of a WPF program.
- It works the same for both WPF XBAP and WPF Window Applications
- Application defines a Run method that keeps the application alive and dispatches messages appropriately
- [STAThread]public static void Main(){
Application app = new Application();
MainWindow window = new MainWindow();
window.Show();
app.Run(window);} - Application also defines a StartupUri property that provides an alternate means of showing the application’s first Window.[STAThread]public static void Main(){
Application app = new Application();
Application.Current.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
Application.Current.Run();} - It offers the possibility to hook into events and override virtual methods for your application
- It acts similar to how the global.asax file manages contextual session and application information for ASP.NET applications
- Activated event /OnActivated virtual method: A window activated by the system
- Deactivated/Deactivated : A window deactivated by the system
- SessionEnding/OnSessionEnding: Raised when a user session ends or the operating, system terminates (i.e., logoff or shutdown)
- ReasonSessionEnding enumeration
- Exit/OnExit: Raised after all windows are destroyed
- Startup/OnStartup: Occurs prior to any windows being created or navigations being performed
- The Application object is defined by the MyApp.xaml file and its corresponding code-behind file.
- System.Windows.Application.Current
- the instance of application
- it is available once it is created
- The use of the Application object to share variables across pages is fundamental to its purpose
- Application.Current.Properties["MyName"] = "George Lache";
- Application defines a readonly Windows collection to give you access to all open Windows.
- MainWindow property
- A WPF application’s main window is the top-level window that is set in the MainWindow property of the Application object.
- This property is set by default when the first instance of the Window class is created and the Application.Current property is set
- This setting can be overrided
- Application.Current.Windows
- The Application object’s Windows property get a list of each top-level Window
- ShutdownMode property
- OnLastWindowClose = 0 - it is the default one
- OnMainWindowClose = 1
- OnExplicitShutdown = 2
- an application can also be shut down manually by calling the Application object’s Shutdown method
- Application.Current.Shutdown()
- How can I create a single-instance application using WPF?
bool mutexIsNew;
using (System.Threading.Mutex m = new System.Threading.Mutex(true, uniqueName, out mutexIsNew))
{
if (mutexIsNew)// This is the first instance. Run the application.else// There is already an instance running. Exit!
}
- A typical WPF application has a single UI thread and a render thread.
- The render thread is an implementation detail that is never directly exposed to developers. It runs in the background and handles low-level tasks such as composition
- DispatcherObject defines a Dispatcher property (of type Dispatcher) containing several overloads of Invoke (a synchronous call) and BeginInvoke (an asynchronous call).
ApplicationSettingsBase
- it can be used to manage user and application settings between application sessions
- supports data change notifications
- we can bind to settings data just like any other data
<Window ... >...<TextBlock ...Text="{Binding Path=LastView,Source={x:Static local:Properties.Settings.Default}}" />...</Window>
Niciun comentariu:
Trimiteți un comentariu