When developing WPF
applications, 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 iOS
development, this is also very common, generally used delegate
or block
callback. The WPF
same 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 WPF
how to call back in China.
Delegate
C#
The Delegate
similar C
or C++
pointer function. Delegate
A reference type variable that holds a reference to a method. References can be changed at runtime. Delegate
Especially used to implement events and callback methods. All Delegate
are derived from the System.Delegate
class. Affirm one delegate
as follows:
public delegate void UpdateMainwindow(string Content);
Event
delegate
Generally and event
simultaneously, use delegation through events. Create one event
as 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