Cheap Dedicated Server -Application Management,The Application Definition
The Application Definition
Implementing an Application Definition
A typical WPF application definition is implemented using both markup and code-behind. This allows you to use markup to declaratively set application properties, resources, and register events, while handling events and implementing application-specific behavior in code-behind.
The following example shows how to implement an application definition using both markup and code-behind:
XAML
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.App" />
C# VB
using System.Windows; // Application
namespace SDKSample
{
public partial class App : Application { }
}
To allow a markup file and code-behind file to work together, the following needs to happen:
In markup, the Application element must include the x:Class attribute. When the application is built, the existence of x:Class in the markup file causes MSBuild to create a partial class that derives from Application and has the name that is specified by the x:Class attribute. This requires the addition of an XML namespace declaration for the XAML schema (xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml").
In code-behind, the class must be a partial class with the same name that is specified by the x:Class attribute in markup and must derive from Application. This allows the code-behind file to be associated with the partial class that is generated for the markup file when the application is built (see Building a WPF Application (WPF)).
This code is the minimum that is required to implement an application definition. However, an additional MSBuild configuration needs to be made to the application definition before building and running the application.
Configuring the Application Definition for MSBuild:
Standalone applications and XAML browser applications (XBAPs) require the implementation of a certain level of infrastructure before they can run. The most important part of this infrastructure is the entry point. When an application is launched by a user, the operating system calls the entry point, which is a well-known function for starting applications.
Traditionally, developers have needed to write some or all of this code for themselves, depending on the technology. However, WPF generates this code for you when the markup file of your application definition is configured as an MSBuild ApplicationDefinition item, as shown in the following MSBuild project file:
<Project
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ApplicationDefinition Include="App.xaml" />
<Compile Include="App.xaml.cs" />
</Project>
Because the code-behind file contains code, it is marked as an MSBuild Compile item, as is normal.
The application of these MSBuild configurations to the markup and code-behind files of an application definition causes MSBuild to generate code like the following:
C# VB
using System; // STAThread
using System.Windows; // Application
namespace SDKSample
{
public class App : Application
{
public App() { }
[STAThread]
public static void Main()
{
// Create new instance of application subclass
App app = new App();
// Code to register events and set properties that were
// defined in XAML in the application definition
app.InitializeComponent();
// Start running the application
app.Run();
}
public void InitializeComponent()
{
}
}
}
The resulting code augments your application definition with additional infrastructure code, which includes the entry-point method Main. The STAThreadAttribute attribute is applied to the Main method to indicate that the main UI thread for the WPF application is an STA thread, which is required for WPF applications. When called, Main creates a new instance of App before calling the InitializeComponent method to register the events and set the properties that are implemented in markup. Because InitializeComponent is generated for you, you don't need to explicitly call InitializeComponent from an application definition like you do for Page and Window implementations. Finally, the Run method is called to start the application.
You may like My previous post :
Cheap Dedicated Server -Management
Cheap Dedicated Server -Application Management,The Application Definition
Reviewed by Cheap VPS
on
00:16
Rating:
No comments: