Wpf c# là gì

In this article

By completing this tutorial, you"ll become familiar with many of the tools, dialog boxes, and designers that you can use when you develop applications with Visual Studio. You"ll create a "Hello, World" application, kiến thiết the UI, add code, and debug errors, while you learn about working in the integrated development environment (IDE).

Bạn đang xem: Wpf c# là gì

Prerequisites


If you haven"t already installed Visual Studio, go khổng lồ the Visual Studio downloads page to install it for không lấy phí.


You can use either .NET Framework or .NET Core for this tutorial. .NET bộ vi xử lý Core is the newer, more modern framework. .NET Core requires Visual Studio 2019 version 16.3 or later.

When you open Visual Studio for the first time, you"ll be prompted khổng lồ sign in. This step is optional for this tutorial. Next you may be shown a dialog box that asks you to lớn choose your development settings & color theme. Keep the defaults & choose Start Visual Studio.

*

After Visual Studio launches, you"ll see tool windows, the menus và toolbars, & the main window space. Tool windows are docked on the left & right sides of the application window, with Quiông chồng Launch, the menu bar, and the standard toolbar at the top. In the center of the application window is the Start Page. When you load a solution or project, editors & designers appear in the space where the Start Page is. When you develop an application, you"ll spover most of your time in this central area.

*


When you launch Visual Studio, the start window opens first. Select Continue without code to open the development environment. You"ll see tool windows, the menus và toolbars, & the main window space. Tool windows are docked on the left và right sides of the application window, with a tìm kiếm box, the thực đơn bar, và the standard toolbar at the top. When you load a solution or project, editors và designers appear in the central space of the application window. When you develop an application, you"ll spkết thúc most of your time in this central area.


Create the project

When you create an application in Visual Studio, you first create a project & a solution. For this example, you"ll create a Windows Presentation Foundation (WPF) project.


Create a new project. On the thực đơn bar, select File > New > Project.

*

In the New Project dialog, select the Installed > Visual C# > Windows Desktop category, and then select the WPF App (.NET Framework) template. Name the project HelloWPFApp, and select OK.

*


Open Visual Studio 2019.

On the start window, choose Create new project.

*

On the Create a new project screen, tìm kiếm for "WPF," choose WPF Application, và then choose Next.

*

At the next screen, give sầu the project a name, HelloWPFApp, and choose Next.

*

In the Additional information window, .NET Chip Core 3.1 should already be selected for your target framework. If not, select .NET Core 3.1. Then, choose Create.

*


Visual Studio creates the HelloWPFApp project và solution, & Solution Explorer shows the various files. The WPF Designer shows a kiến thiết view và a XAML view of MainWindow.xaml in a split view. You can slide the splitter to lớn show more or less of either view. You can choose to see only the visual view or only the XAML view.

*


Note

For more information about XAML (eXtensible Application Markup Language), see the XAML overview for WPF page.


After you create the project, you can customize it. To vị so, choose Properties Window from the View thực đơn, or press F4. Then, you can display & change options for project items, controls, and other items in an application.

*

Change the name of MainWindow.xaml

Let"s give MainWindow a more specific name. In Solution Explorer, right-clichồng on MainWindow.xaml và choose Rename. Rename the file to lớn Greetings.xaml.

Design the user interface (UI)

If the designer is not open, select Greetings.xaml và press Shift+F7 lớn open the designer.

We"ll add three types of controls to lớn this application: a TextBlock control, two RadioButton controls, & a Button control.

Add a TextBlochồng control

Press Ctrl+Q to activate the tìm kiếm box & type Toolbox. Choose View > Toolbox from the results list.

In the Toolbox, expand the Comtháng WPF Controls node to lớn see the TextBloông xã control.

*

Add a TextBloông chồng control lớn the kiến thiết surface by choosing the TextBlock công trình and dragging it khổng lồ the window on the kiến thiết surface. Center the control near the top of the window. In Visual Studio 2019 & later, you can use the red guidelines to lớn center the control.

Your window should resemble the following illustration:

*

The XAML markup should look something lượt thích the following example:

Customize the text in the text block

In the XAML view, locate the markup for TextBlock & change the Text attribute from TextBox khổng lồ Select a message option và then choose the Display button.

The XAML markup should look something lượt thích the following example:

Center the TextBloông chồng again if you like, and then save your changes by pressing Ctrl+S or using the File menu tác phẩm.

Next, you"ll add two RadioButton controls lớn the form.

Xem thêm: Nấm Nguyệt Ảnh Vàng Quái Vật Trong Phụ Bản Nào? Vườn Thiên Quy

Add radio buttons

In the Toolbox, find the RadioButton control.

*

Add two RadioButton controls khổng lồ the thiết kế surface by choosing the RadioButton tác phẩm & dragging it lớn the window on the kiến thiết surface. Move sầu the buttons (by selecting them và using the arrow keys) so that the buttons appear side by side under the TextBloông xã control. Use the red guidelines to align the controls.

Your window should look lượt thích this:

*

In the Properties window for the left RadioButton control, change the Name property (the property at the top of the Properties window) to lớn HelloButton.

*

In the Properties window for the right RadioButton control, change the Name property khổng lồ GoodbyeButton, & then save your changes.

Next, you"ll add display text for each RadioButton control. The following procedure updates the Content property for a RadioButton control.

Add display text for each radio button

Update the Content attribute for the HelloButton & GoodbyeButton to lớn "Hello" & "Goodbye" in the XAML. The XAML markup should now look similar to the following example:

Set a radio button to lớn be checked by default

In this step, we"ll set HelloButton to be checked by mặc định so that one of the two radio buttons is always selected.

In the XAML view, locate the markup for HelloButton.

Add an IsChecked attribute and mix it khổng lồ True. Specifically, add IsChecked="True".

The XAML markup should now look similar lớn the following example:

The final UI element that you"ll add is a Button control.

Add the button control

In the Toolbox, find the Button control, and then add it lớn the thiết kế surface under the RadioButton controls by dragging it to the khung in the thiết kế view. If you"re using Visual Studio 2019 or later, a red line helps you center the control.

In the XAML view, change the value of Content for the Button control from Content="Button" khổng lồ Content="Display", & then save the changes.

Your window should resemble the following illustration.

*

The XAML markup should now look similar lớn the following example:

Add code lớn the display button

When this application runs, a message box appears after a user chooses a radio button and then chooses the Display button. One message box will appear for Hello, & another will appear for Goodbye. To create this behavior, you"ll add code lớn the Button_Click sự kiện in Greetings.xaml.cs.

On the thiết kế surface, double-cliông xã the Display button.

Greetings.xaml.cs opens, with the cursor in the Button_Clichồng sự kiện.

private void Button_Click(object sender, RoutedEventArgs e)Enter the following code:

if (HelloButton.IsChecked == true) MessageBox.Show("Hello.");else if (GoodbyeButton.IsChecked == true) MessageBox.Show("Goodbye.");Save the application.

Debug & demo the application

Next, you"ll debug the application lớn look for errors and demo that both message boxes appear correctly. The following instructions tell you how to lớn build & launch the debugger, but later you might read Build a WPF application (WPF) and Debug WPF for more information.

Find and fix errors

In this step, you"ll find the error that we caused earlier by changing the name of the MainWindow.xaml tệp tin.

Start debugging and find the error

Start the debugger by pressing F5 or selecting Debug, then Start Debugging.

A Break Mode window appears, and the Output window indicates that an IOException has occurred: Cannot locate resource "mainwindow.xaml".

*

Stop the debugger by choosing Debug > Stop Debugging.

We renamed MainWindow.xaml to Greetings.xaml at the start of this tutorial, but the code still refers to MainWindow.xaml as the startup URI for the application, so the project can"t start.

Specify Greetings.xaml as the startup URI

In Solution Explorer, open the App.xaml file.

Change StartupUri="MainWindow.xaml" to lớn StartupUri="Greetings.xaml", and then save sầu the changes.

Xem thêm: Tải Game My Talking Tom 2 Hack Game Mèo Tom 2 Hack Apk :: Tiqua

Start the debugger again (press F5). You should see the Greetings window of the application.


Chuyên mục: Công Nghệ