📜  WPF-Hello World(1)

📅  最后修改于: 2023-12-03 14:48:34.942000             🧑  作者: Mango

WPF - Hello World

Introduction

Windows Presentation Foundation (WPF) is a graphical subsystem for building Windows desktop applications. It provides a unified programming model for creating interactive and visually stunning user interfaces. In this guide, we will walk you through the process of creating a "Hello World" application using WPF.

Prerequisites
  • Basic understanding of C# programming language.
  • Visual Studio installed on your machine.
Step 1: Create a new WPF project
  1. Open Visual Studio and click on "Create a new project".
  2. Select "WPF App" under the C# section.
  3. Choose a suitable name and location for your project, and click "Create".
Step 2: Design the user interface
  1. In the Solution Explorer, open the MainWindow.xaml file.
  2. Replace the existing XAML code with the following:
<Window x:Class="WpfHelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Hello World" Height="350" Width="525">
    <Grid>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24">
            Hello World!
        </TextBlock>
    </Grid>
</Window>
Step 3: Implement the code-behind
  1. In the Solution Explorer, open the MainWindow.xaml.cs file.
  2. Replace the existing code with the following:
using System.Windows;

namespace WpfHelloWorld
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
Step 4: Run the application
  1. Hit F5 or click on the "Start Debugging" button to run the application.
  2. You should see a window with the text "Hello World!" displayed in the center.

Congratulations! You have successfully created a "Hello World" application using WPF.

Conclusion

WPF is a powerful framework for building Windows desktop applications with rich user interfaces. This guide provided you with a basic introduction to creating a simple WPF application. Explore further to unleash the full potential of WPF and build more complex and feature-rich applications.