WPF-Window data callback

Original: WPF-Window Data Callback

When developing WPFapplications, you will often encounter such a scenario. Clicking a button on the main window will pop up a sub-window with an input box in the sub-window. After the input is complete, then click the [OK] button The content in the input box is transferred to the main window. In iOSdevelopment, this is also very common, generally used delegateor blockcallback. The WPFsame is true in China, you can use the delegate to call back, from which we can see that the technology is interlinked . Let's take a look at WPFhow to call back in China.

Delegate

C#The Delegatesimilar Cor C++pointer function. DelegateA reference type variable that holds a reference to a method. References can be changed at runtime. DelegateEspecially used to implement events and callback methods. All Delegateare derived from the System.Delegateclass. Affirm one delegateas follows:

public delegate void UpdateMainwindow(string Content);

Event

delegateGenerally and eventsimultaneously, use delegation through events. Create one eventas follows:

public event UpdateMainwindow updateMainwindow;

Call delegate

When you click a button in the subview and need to pass some information to the main interface, you need to call the event in the subview as follows:

private void Add_Button_Click(object sender, RoutedEventArgs e)
        {
            if (updateMainwindow != null)
            {
                updateMainwindow("WinAddPlaneLine");
            }
            this.Close();
        }

Then when creating a child window in the main view, you need to expose the delegate of the child window to the main window as follows:

WinAddField winAddField = new WinAddField();
winAddField.updateMainwindow += SubWindow_updateMainwindow;

private void SubWindow_updateMainwindow(string Content)
        {
            Message.show(Content);
        }

Follow the above steps to complete the data callback, using pseudo code, just a brief introduction to how delegates and events are used to handle data callbacks.
personal blog

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12709870.html
Recommended