Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define events in C #

Shulou Source: shulou.com Published: 2022-06-02 09:07:55 09月27日 Update

The editor would like to share with you how C# defines events. I hope you will gain something after reading this article. Let's discuss it together.

C # defines event application

Recently, in the last wpf project of the company, students familiar with WPF know that the concept of "user control" is very common in WPF controls, and we often have to do some controls to achieve some relatively complex functions, such as: a two-dimensional warehouse management system, the shelf in the warehouse can be made into a user control, and a shelf layer in the shelf, a shelf in the shelf layer In fact, it can be a user control. When we draw a specific shelf, we can read the relevant data from the database and generate two-dimensional shelf graphics with several grids and layers according to the actual situation of the shelf. Because the shelf is implemented through several layers of user controls, sometimes we need to pass messages in their "hierarchy". For example, if the information of one of my shelves has changed, we need to notify the entire shelf or even load a window of the shelf. at this time, C# can define the event application, throwing the event up from a "layer" that triggers the event, and the parent control receives the event. Then throw it up until some layer that receives the event makes a specific event handling.

First of all, let's make a simple user control to simulate the graphical control that triggers events in the * layer:

UsingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Windows; usingSystem.Windows.Controls; usingSystem.Windows.Data; usingSystem.Windows.Documents; usingSystem.Windows.Input; usingSystem.Windows.Media; usingSystem.Windows.Media.Imaging; usingSystem.Windows.Navigation; usingSystem.Windows.Shapes; namespaceWpfApplication5 {/ Interactionlogicforuc1.xaml / publicpartialclassuc1:UserControl {publicuc1 () {InitializeComponent ();} privatestring_name; publicstringName {get Set;}} publicclassuc1ClickEventArgs {publicstringName {get; set;}

The uc1ClickEventArgs class is a custom event parameter class, which is used to hold some information about this control for its parent container to call.

Then there is a user control, which is used to install multiple graphical controls on the top. For example, we can think of it as a shelf, and below is a shelf. I use the most basic loop to generate several user controls in the image above:

Code usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Windows; usingSystem.Windows.Controls; usingSystem.Windows.Data; usingSystem.Windows.Documents; usingSystem.Windows.Input; usingSystem.Windows.Media; usingSystem.Windows.Media.Imaging; usingSystem.Windows.Navigation; usingSystem.Windows.Shapes; namespaceWpfApplication5 {/ Interactionlogicforwhs_map.xaml / publicdelegatevoidtestDelegate (objectsender,uc1ClickEventArgse) Publicpartialclasswhs_map:UserControl {publicwhs_map () {InitializeComponent ();} privateeventtestDelegate_testEvent; publiceventtestDelegatetestEvent {add {_ testEvent+=value;} remove {_ testEvent-=value;}} privatevoidUserControl_Loaded (objectsender,RoutedEventArgse) {intleft=5; inttop=1; for (inti=0;i)

Tags: Controls events shelves users clients graphics warehouses information articles 2D applications generation complexity diagrams superiors companies functions parameters classmates multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS MySQL MariaDB OPPO Reno