Do you know the difference between WPF and Winforms?

Do you know the difference between WPF and Winforms?

Introduce the main differences between the two methods of developing Windows desktop applications, which can play a better role in modern system development.

最后更新 4/7/2021 11:59 PM
沙漠尽头的狼
预计阅读 7 分钟
分类
WPF Winform
标签
.NET C# WPF Winform

introduced

The abbreviation for WPF refers to Microsoft's Windows Presentation Foundation, while WinForms is a simple combination of Windows Forms Applications. Both are Microsoft's Windows application graphical user interfaces that developers can use to develop Windows desktop applications. This article focuses on the main differences between two methods for developing Windows desktop applications, which can play a better role in modern system development.

Windows Forms

WinForms was introduced in February 2002 as part of the. NET Framework. For the most part, WinForms allows developers to drag and drop controls on Windows Forms and allows developers to manipulate these controls using code-behind files that can have C#, VB. NET, or any other. NET language. Each WinForms control is an instance of a class because WinForms exists as a wrapper with a set of C++ classes. Microsoft's Visual Studio makes developing WinForms easier because developers can easily drag and drop controls from the toolbox.

Controls in the WinForms toolbox:

WinForms工具箱中的控件

In WinForms desktop applications, developers can only access code-behind files in which they can manipulate control events. WinForms desktop applications have limitations in terms of the functionality of controls and application behavior, which will be revealed in the next section.

WPF desktop application

Unlike WinForms, WPF's architecture consists of three main components: a presentation framework, presentation core, and mallcore. WPF does not rely entirely on standard Windows controls and is therefore a stand-alone approach. In 2007, Microsoft introduced Windows Presentation Foundation (WPF) to alternate WinForms for. NET Framework desktop application development. This alternation brings many changes in desktop application development. First, WPF separates designers and programmers, and can use Visual Studio or Blend to design UIs separately, while developers can use code-behind files to manipulate control events.

WPF uses XAML to create controls, whose file structure is more like ASP.NET. You are free to use the designer or write XAML code to create controls. Designers using Canvas Panel can still drag and drop controls on Windows pages as they do in WinForms. The main differences WPF brings are XAML files and access to the visible designers that come with the XAML files.

WPF visual design and XAML file editing:

WPF可视化设计和XAML文件编辑

The figure above shows the layout of the WPF application, with the XAML file displayed next to the Designer.

The file structure of the WPF project is as follows:

WPF项目的文件结构

  • Each window or page has a.xaml file for adding controls and a.cs,.vb file, etc., which is a code-behind file, more like the ASP.NET approach.
  • Unlike WinForms, WPF generates an initial MainWindow to launch the application, and to change the launch window, you can do this in the App.xaml file.

WPF main form startup configuration:

WPF主窗体启动配置

  • This file serves as an entry for the application.

The other significant difference between WPF and WinForms is the controls. To add controls, you just need to write simple XAML code. For example, to add a text box to a WPF window, you can write the following code:

<Window
  x:Class="WpfApp1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:WpfApp1"
  mc:Ignorable="d"
  Title="MainWindow"
  Height="450"
  Width="800"
>
  <StackPanel>
    <TextBox></TextBox>
  </StackPanel>
</Window>

Note the markup in the syntax, which recommends the name "Extended Application Markup Language (XAML)". XAML code is placed in Window tags. Control labels may have properties that describe the control's width, height, etc., depending on the control.

WPF also brings another significant difference from WinForms, which is the ability to add Buttons with images. In WinForms, adding an image to a button means you have to draw the image yourself or include some third-party controls, but the WPF button control is simple and you can add anything to it.

<Window
  x:Class="WpfApp1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:WpfApp1"
  mc:Ignorable="d"
  Title="MainWindow"
  Height="500"
  Width="800"
>
  <button Padding="5">
    <StackPanel Orientation="Horizontal">
      <image Source="/Image.jpg" Height="25" Width="50" />
      <TextBlock Margin="5,0">I'm a Button</TextBlock>
    </StackPanel>
  </button>
</Window>

The output is as follows: WPF Run Demonstration

WPF运行演示

WPF also provides fully supported data binding capabilities, as shown in the following example:

<Window
  x:Class="WpfApp1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:WpfApp1"
  mc:Ignorable="d"
  Title="MainWindow"
  Height="500"
  Width="800"
>
  <StackPanel Margin="10">
    <WrapPanel Margin="0,10">
      <label Content="Your Text Here:" FontWeight="Bold" />
      <TextBox
        Name="txtBind"
        Height="20"
        Width="250"
        RenderTransformOrigin="-2.75,0.587"
        Margin="59,0,336,0"
      />
    </WrapPanel>
    <WrapPanel Margin="0,10">
      <TextBlock Text="Bound-Text: " FontWeight="Bold" />
      <TextBlock Text="{Binding Path=Text, ElementName=txtBind}" />
    </WrapPanel>
  </StackPanel>
</Window>

Output: WPF data binding demonstration

WPF数据绑定演示

The property in the above example is used to bind text in TextBlock to text in txtBindTextBox. This just shows how easy it is to bind data in WPF using the attribute.

conclusion

This article demonstrates the main differences between WinForms and WPF through differences in architecture, syntax, file structure, and application behavior between the two. NET ways of creating desktop applications. Although WinForms design may seem friendly and straightforward, XAML brings some useful features that developers may need in modern desktop applications.

Original link: www.c-sharpcorner.com/article/wpf-vs-winforms/

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/7/2022

What is the difference between Winform and WPF?

There are always friends who ask,"What is the difference between WinForm and WPF?" Thinking about this question seems to be a simple answer, but it has never been systematically analyzed. Today, I took the time to write an article record summary that only represents my personal opinion.

继续阅读