By convention, create a WPF application, click the button and display "Hello WPF World" in the text box, which we use to analyze the structure of the WPF application.
The XAML file is as follows:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation& quot;
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPFHelloWorld.MainWindow"
x:Name="Window"
Title="WPF Hello World"
Width="300" Height="130"
WindowStartupLocation="CenterScreen">
<StackPanel Margin="10,10,10,10" >
<TextBox x:Name="txtValue" Width="200" />
<Button x:Name="btnOK"
Content="OK" Width="50"
Click="btnOK_Click">
<Button.Margin>
<Thickness Top="10" />
</Button.Margin>
</Button>
</StackPanel>
</Window>
The C # code is as follows:
using System.Windows;
namespace WPFHelloWorld
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
Window.ResizeMode = ResizeMode.NoResize;
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
txtValue.Text = "Hello WPF World!";
}
}
}
Program execution effect as shown in figure:
The code that analyzes this project gets the following points:
With windows-based applications, Web application types, and WPF forms also belong to typed forms, where each form inherits from the System.Windows.Window class
For the properties of a WPF control, you can declare the setting in a XAML file, or you can set it in code (this is similar to the ASP.net WebControl property setting)
There are two namespaces in the XAML file Windows declaration
xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presen tation": Maps the entire Windows presentation Foundation (WPF) namespace to Shoot as default namespace
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"; : Maps a separate Extensible Application Markup Language (XAML) namespace, which is typically mapped to x: Prefix
Note that these two namespaces refer to URIs rather than URLs that actually exist.
For some properties of a control, you can set it directly, or you can set it separately. Many WPF types or members of these types extend basic string property processing behavior, so instances of more complex object types can be specified as property values by strings. At the code level, this process is done by specifying a CLR type converter that handles string property values. such as margin. In this case, the StackPanel margin property is given directly as a string, and the button's margin is given through a CLR mapping.
If you use a control object in your code, the object needs to be specified x:name in the XAML declaration, such as a property or method that does not require access to the control object in your code, you do not need to specify
The x:class in the window declaration specifies the background class that corresponds to the XAML file, and the event is handled in a class that is written in the
The event handling used by WPF is similar to ASP.net, where event bindings are made at the control declaration location of the XAML file, and the specific code for the event-handling method is given in the background class