C # WPF enables the transfer of values between multiple windows through delegation

Source: Internet
Author: User

When using WPF development, you will inevitably encounter the need for two of Windows to carry out the value of the operation, of course, there are many ways to transfer values between multiple windows, this article describes the use of delegates to achieve the value of multiple windows.

Before the code, let's briefly describe what is a delegate in C # (if you just want to know how to pass the value can skip this part) on the network there are a lot of delegates on the introduction and explanation, after my study and summed up a bit of my own understanding, I think the delegate is a C language similar to the pointer, But it points to a method, not a variable. If you think of a delegate as a variable, then this variable has the address of your target method, which is about equal to calling your target method . (Personal understanding Welcome to communicate)

The following text:

To achieve the mutual value between the windows, first create two windows, first the Code main window code:

MainWindow.xaml

<Grid> <textbox name="Mainwindowtextbox"Horizontalalignment=" Left"height=" at"margin="10,61,0,0"textwrapping="Wrap"text="Empty"Verticalalignment="Top"Width="297"/> <button content="Open a new window"Horizontalalignment=" Left"margin="10,130,0,0"Verticalalignment="Top"Width="297"click="Buttonbase_onclick"/></grid>

MainWindow.xaml.cs

1  Public voidGetValue (stringvalue1, TextBox value2)2     {3Mainwindowtextbox.text =value1;4     }5 6Private voidButtonbase_onclick (Objectsender, RoutedEventArgs e)7     {8Window1 NewWindow1 =NewWindow1 ();9Newwindow1.gettexthandler = GetValue;//assigning a method to a delegate objectTen       Newwindow1.showdialog (); One  A}

As follows:

The second window Window1 code:

Window1.xaml

<Grid> <textbox name="Window1textbox"Horizontalalignment=" Left"height=" at"margin="84,73,0,0"textwrapping="Wrap"text=""Verticalalignment="Top"Width=" -"/> <button content="Pass Value"Horizontalalignment=" Left"margin="84,125,0,0"Verticalalignment="Top"Width=" -"click="Buttonbase_onclick"/></grid>

Window1.xaml.cs

1  Public Delegate voidGettexthandler (stringValue1, TextBox value2);//declaring a delegate2  PublicGettexthandler Gettexthandler;//Delegate Object3 4 Private voidButtonbase_onclick (Objectsender, RoutedEventArgs e)5     {6       Gettexthandler (Window1textbox.text, window1textbox);7}

Implementation after running the program, click the Open New Window button, the Window1 window will open, in the Window1 window of the textbox to enter content, click on the value, you enter the content will be uploaded to the main window, Change the contents of the TextBox control in the main window to the value you passed through the delegate's event. The effect is as follows:

Now we have implemented the transfer of values between the Windows. Next I will briefly introduce the implementation of the above code and some of their own understanding, if not interested, or will already use the delegate to the Multi-window transfer value, the latter part can be skipped.

Front code is not introduced in this first, in the MainWindow.xaml.cs file

 Public void GetValue (string value1, TextBox value2)

This method is the target method for the delegate, this method returns a null value, or it can set its return value, and when the delegate is used, it also receives the return value of the target method. Then there is this method to receive two parameters, one is the string is a TextBox, the second parameter is not really meaningful, just to show that the variables passed here can be multiple, or other object types.

Newwindow1.gettexthandler = GetValue;          // assigning a method to a delegate object

Assigning a method to a delegate object can be understood as the target method of the Gettexthandler that binds the two of them together is getvalue.

In the Windo1.xaml.cs:

 Public Delegate void Gettexthandler (string value1, TextBox value2);  // declaring a delegate  Public Gettexthandler Gettexthandler;                                // Delegate Object

Delegate is the keyword that declares the delegate, where the return value is NULL, if the target method has a return value, in this case the return value is written in the same type, and the two variable types received are also consistent with the target method.

The next step is to define the delegate object, the uppercase Gettexthandler is the delegate, and the lowercase gettexthandler is the object, and the lowercase gettexthandler usage is used when the delegate is used:

Gettexthandler (Window1textbox.text, Window1textbox);

Writing to the end, the use of a lot of delegates, such as: Event subscription, anonymous methods, multicast delegation and so on, this article is about the basic use of the delegate, the other usage is still learning, welcome to communicate.

2018.8.30 three o'clock in the afternoon difference five minutes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.