Wpf c# là gì

Feedbaông xã will be sent to lớn motoavangard.com: By pressing the submit button, your feedbachồng will be used lớn improve sầu motoavangard.com products and services. Privacy policy.

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


In this article

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

Prerequisites


If you haven"t already installed Visual Studio, go to the Visual Studio downloads page lớn install it for miễn phí.


You can use either .NET Framework or .NET Vi xử lý 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 lớn sign in. This step is optional for this tutorial. Next you may be shown a dialog box that asks you khổng lồ choose your development settings & color theme. Keep the defaults and 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 thực đơn bar, & 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 và designers appear in the space where the Start Page is. When you develop an application, you"ll spend 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 & toolbars, & the main window space. Tool windows are docked on the left and right sides of the application window. The tìm kiếm box, menu bar, và the standard toolbar are located at the top. When you load a solution or project, editors & designers appear in the central space of the application window. When you develop an application, you"ll spend most of your time in this central area.


Create the project

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


Create a new project. On the menu bar, select File > New > Project.

*

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

*


Open Visual Studio.

On the start window, choose Create new project.

*

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

*

At the next screen, give the project a name, HelloWPFApp, và choose Next.

*

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

*

Visual Studio creates the HelloWPFApp project & solution, & Solution Explorer shows the various files. The WPF Designer shows a thiết kế view and a XAML view of MainWindow.xaml in a split view. You can slide the splitter khổng lồ show more or less of either view. You can choose khổng lồ 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 do so, choose Properties Window from the View menu, or press F4. Then, you can display và change options for project items, controls, và other items in an application.

*


mở cửa Visual Studio.

On the start window, choose Create a new project.

*

On the Create a new project screen, search for "WPF," choose WPF Application, và then choose Next.

*

At the next screen, give the project a name, HelloWPFApp, & choose Next.

*

In the Additional information window, .NET 6.0 (Long-term support) should already be selected for your target framework. If not, select .NET 6.0 (Long-term support). Then, choose Create.

*

Visual Studio creates the HelloWPFApp project và solution, and Solution Explorer shows the various files. The WPF Designer shows a thiết kế view & a XAML view of MainWindow.xaml in a split view. You can slide the splitter lớn show more or less of either view. You can choose khổng lồ 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ày so, choose Properties Window from the View menu, or press F4. Then, you can display và change options for project items, controls, and other items in an application.

*


Change the name of MainWindow.xaml

Let"s give sầu MainWindow a more specific name. In Solution Explorer, right-clichồng on MainWindow.xaml & choose Rename. Rename the tệp tin lớn Greetings.xaml.

Design the user interface (UI)

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

We"ll add three types of controls khổng lồ this application: a TextBloông chồng control, two RadioButton controls, & a Button control.

Add a TextBlock control


Press Ctrl+Q lớn activate the tìm kiếm box & type Toolbox. Choose View > Toolbox from the results menu.

In the Toolbox, expand the Common WPF Controls node lớn see the TextBlochồng control.

*

Add a TextBloông xã control khổng lồ the thiết kế surface by choosing the TextBlock item và 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 and later, you can use the red guidelines lớn center the control.

Your window should resemble the following illustration:

*

The XAML markup should look something like the following example:


Press Ctrl+Q to lớn activate the tìm kiếm box và type Toolbox. Choose View > Toolbox from the results list.

In the Toolbox, exp& the Common WPF Controls node khổng lồ see the TextBlochồng control.

*

Add a TextBloông chồng control to the thiết kế surface by choosing the TextBlock nhà cửa and dragging it to lớn the window on the thiết kế surface. Center the control near the top of the window. You can use the guidelines to lớn center the control.

Your window should resemble the following image:

*

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 and change the Text attribute from TextBox to Select a message option & 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 lượt thích, và then save your changes by pressing Ctrl+S or using the File menu nhà cửa.

Next, you"ll add two RadioButton controls khổng lồ the khung.

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 thắng lợi và dragging it to the window on the kiến thiết surface. Move the buttons (by selecting them & using the arrow keys) so that the buttons appear side by side under the TextBloông xã control. Use the red guidelines to lớn 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 to lớn GoodbyeButton, và then save sầu your changes.

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


In the Toolbox, find the RadioButton control.

*

Add two RadioButton controls to the kiến thiết surface by choosing the RadioButton thành phầm và dragging it lớn the window on the kiến thiết surface. Move the buttons (by selecting them and using the arrow keys) so that the buttons appear side by side under the TextBloông chồng control. You can use the guidelines lớn 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) khổng lồ HelloButton.

*

In the Properties window for the right RadioButton control, change the Name property lớn GoodbyeButton, & then save sầu 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 khổng lồ "Hello" and "Goodbye" in the XAML. The XAML markup should now look similar to lớn the following example:

Set a radio button khổng lồ be checked by default

In this step, we"ll mix HelloButton to lớn be checked by default so that one of the two radio buttons is always selected.

In the XAML view, locate the markup for HelloButton.

Add an IsChecked attribute và phối it lớn True. Specifically, add IsChecked="True".

The XAML markup should now look similar to 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 khổng lồ the design surface under the RadioButton controls by dragging it khổng lồ the khung in the kiến thiết 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" to lớn Content="Display", và then save the changes.

Your window should resemble the following illustration.

*

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


In the Toolbox, find the Button control, và then add it khổng lồ the kiến thiết surface under the RadioButton controls by dragging it to the size in the thiết kế view. The guidelines can help you center the control.

In the XAML view, change the value of Content for the Button control from Content="Button" lớn Content="Display", and then save sầu the changes.

Your window should resemble the following screenshot.

*

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


Add code khổng lồ the display button


When this application runs, a message box appears after a user chooses a radio button & then chooses the Display button. One message box will appear for Hello, and another will appear for Goodbye. To create this behavior, you"ll add code to the Button_Cliông xã event in Greetings.xaml.cs.

On the kiến thiết surface, double-cliông chồng the Display button.

Xem thêm: Danh Tướng 3Q: Hướng Dẫn Cách Chơi 3Q Pro Tất Cả Các Tướng Trong 3Q

Greetings.xaml.cs opens, with the cursor in the Button_Cliông xã event.

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.


When this application runs, a message box appears after a user chooses a radio button & then chooses the Display button. One message box will appear for Hello, and another will appear for Goodbye. To create this behavior, you"ll add code to the Button_Clichồng event in Greetings.xaml.cs.

On the design surface, double-cliông chồng the Display button.

Greetings.xaml.cs opens, with the cursor in the Button_Cliông xã event.

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 và test the application

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

Find và 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 và find the error

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

A Break Mode window appears, và 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 lớn 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.


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 lớn Greetings.xaml at the start of this tutorial, but the code still refers to lớn 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 tệp tin.

Change StartupUri="MainWindow.xaml" lớn StartupUri="Greetings.xaml", & save the changes.

As an optional step, it will avoid confusion lớn change the title of your application window to match this new name.

In Solution Explorer, open the Greetings.xaml file that you just renamed.

Change the value of the Window.Title property from Title="MainWindow" to lớn Title="Greetings", và save the changes.

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


*


*


*


Now cthua thảm the application window khổng lồ stop debugging.

Debug with breakpoints

You can demo the code during debugging by adding some breakpoints. You can add breakpoints by choosing Debug > Toggle Breakpoint, by clicking in the left margin of the editor next khổng lồ the line of code where you want the break lớn occur, or by pressing F9.

Add breakpoints

Open Greetings.xaml.cs, & select the following line: MessageBox.Show("Hello.")

Add a breakpoint from the thực đơn by selecting Debug, then Toggle Breakpoint.

A red circle appears next to lớn the line of code in the far left margin of the editor window.

Select the following line: MessageBox.Show("Goodbye.").

Press the F9 key lớn add a breakpoint, & then press F5 to start debugging.

In the Greetings window, choose the Hello radio button, and then choose the Display button.

The line MessageBox.Show("Hello.") is highlighted in yellow. At the bottom of the IDE, the Autos, Locals, & Watch windows are docked together on the left side, and the hotline Staông chồng, Breakpoints, Exception Settings, Command, Immediate, & Output windows are docked together on the right side.

*

On the thực đơn bar, choose Debug > Step Out.

The application resumes execution, & a message box with the word "Hello" appears.

Choose the OK button on the message box to lớn cthảm bại it.

In the Greetings window, choose the Goodbye radio button, & then choose the Display button.

The line MessageBox.Show("Goodbye.") is highlighted in yellow.

Choose the F5 key to continue debugging. When the message box appears, choose the OK button on the message box lớn cthua it.

Cthua the application window khổng lồ stop debugging.

On the thực đơn bar, choose Debug > Disable All Breakpoints.


Open Greetings.xaml.cs, và select the following line: MessageBox.Show("Hello.")

Add a breakpoint from the thực đơn by selecting Debug, then Toggle Breakpoint.

A red circle appears next to lớn the line of code in the far left margin of the editor window.

Select the following line: MessageBox.Show("Goodbye.").

Press the F9 key khổng lồ add a breakpoint, and then press F5 to lớn start debugging.

In the Greetings window, choose the Hello radio button, và then choose the Display button.

The line MessageBox.Show("Hello.") is highlighted in yellow. At the bottom of the IDE, the Autos, Locals, và Watch windows are docked together on the left side, và the hotline Staông chồng, Breakpoints, Exception Settings, Commvà, Immediate, & Output windows are docked together on the right side.

*

On the thực đơn bar, choose Debug > Step Out.

The application resumes execution, and a message box with the word "Hello" appears.

Choose the OK button on the message box lớn cthua it.

In the Greetings window, choose the Goodbye radio button, & then choose the Display button.

The line MessageBox.Show("Goodbye.") is highlighted in yellow.

Choose the F5 key to continue debugging. When the message box appears, choose the OK button on the message box to cthua trận it.

Cchiến bại the application window khổng lồ stop debugging.

On the thực đơn bar, choose Debug > Disable All Breakpoints.


View a representation of the UI elements

In the running app, you should see a widget that appears at the top of your window. The widget is a runtime helper that provides quiông xã access lớn some helpful debugging features. Select the first button, Go to Live Visual Tree. You should see a window with a tree that contains all the visual elements of your page. Expvà the nodes lớn find the buttons you added.


*


*


Build a release version of the application

Now that you"ve verified that everything works, you can prepare a release build of the application.

On the main thực đơn, select Build > Clean solution lớn delete intermediate files and output files that were created during previous builds. This step isn"t required, but it cleans up the debug build outputs.

Change the build configuration for HelloWPFApp from Debug to lớn Release by using the dropdown control on the toolbar (it says "Debug" currently).

Build the solution by choosing Build > Build Solution.

Congratulations on completing this tutorial! You can find the .exe you built under your solution and project directory (...HelloWPFAppHelloWPFAppinRelease).

Next steps

Congratulations on completing this tutorial! To learn even more, continue with the following tutorials.