Asp.net event transfer and wpf route proxy instance

Source: Internet
Author: User
Tags bind instance method

Asp.net event transfer and wpf route proxy instance asp.net, event transfer, wpf route event, transfer an article about. net event transfer and the wpf route proxy instance code, the author wrote in great detail, there is a need to know about event transfer and wpf route proxy friends do not prevent access reference.

During these days, the wpf project involves a pop-up window to update the parent ui. At the same time, the pop-up window needs to process certain logic and call the function of the parent window to continue execution.

Here, we use. net event passing, ui event updating, and wpf route events.

1. What is. net event transfer (Event chain)

During. NET program design, events of some helper classes are often encapsulated and bound to a container class (which is assumed as the ClassA class below. Therefore, we often encounter the so-called event transfer or transfer problem: the event of the auxiliary class ClassB is not directly to the customer, but released through its container class ClassA. The following two problems are discussed:

Event binding implementation: differences between non-constructor and constructor

Container Event chain: event accesser + = Add Event linked list when reloading

1. Event binding implementation

1) Non-constructor implementation

If it is the same as the ClassB event delegate, ClassA can directly + = bind the ClassB event and then pass it out for release. See the following code:

The code is as follows: Copy code

Public class ClassB // class ClassB publishes events through the container class ClassA
{
Public event EventHandler ClassBEvent;
Public void Fire ()
{
If (ClassBEvent! = Null)
{
ClassBEvent (this, new EventArgs ());
}
}
}
Public class ClassA // The container class that transmits the events that publish the ClassB.
{
Public event EventHandler ClassAEvent;
Private ClassB classB;
Public ClassA (){}
Public void Fire () // non-constructor
{
ClassB = new ClassB ();
ClassB. ClassBEvent + = this. ClassAEvent; // bind the ClassB event to ClassA
ClassB. Fire ();
}
}

2) constructor implementation problems

If you bind an event to the ClassA constructor, the object is still null because the class object is not fully constructed. In this case, the binding event will be faulty. Refer to the following ClassA code:

The code is as follows: Copy code
Public class ClassA
{
Public event EventHandler ClassAEvent;
Private ClassB classB;

Public ClassA () // bind an event to the constructor
{
ClassB = new ClassB ();
ClassB. ClassBEvent + = new EventHandler (ClassAEvent); // The Class object is still null
}
Public void Fire ()
{
ClassB. Fire ();
}
}

The above code will throw an exception message "unprocessed exception: System. ArgumentException: the delegate of the instance method cannot have null this ." Obviously, this information indicates that the current class object is still null (that is, this is null) and cannot be used as a parameter to create a delegate object.

 

Note: If the so-called delegate inference method is used, the new EventHandler () is not used to create the delegate object, but the event is directly bound:

ClassB. ClassBEvent + = this. ClassAEvent; // delegate inference method
At this time, neither an exception is thrown nor the event registration method of (response) ClassA is called. That is to say, the above method is invalid!

3) constructor implementation solution

If you want to bind an event to the ClassA constructor, you can write a method that complies with the event delegate and then + = the method. The code is as follows:

The code is as follows: Copy code
Public ClassA () // ClassA constructor
{
ClassB = new ClassB ();
ClassB. ClassBEvent + = OnClassAEvent; // bind to a method
}
Private void OnClassAEvent (object sender, EventArgs e) // delegate-compliant method
{
If (this. ClassAEvent! = Null)
{
This. ClassAEvent (sender, e );
}
}

 

 

 

2. The routing event implementation code of WPF:

1) use proxy

Define the proxy in the subwindow:

The code is as follows: Copy code

Public delegate void AddAddEventHandler ();
Public event AddAddEventHandler AddAddEventEvent;

Call in the pop-up window:

The code is as follows: Copy code
Private void Button_Click_1 (object sender, RoutedEventArgs e)
{

Try {
If (SelectedInvestGUID. Count = 0)
{
MessageBox. Show ("at least one object must be selected. ");
Return;
}


If (AddAddEventEvent! = Null) {AddAddEventEvent ();}
}
Catch (Exception ex)
{
MessageBox. Show ("error" + ex );
}
}

Event chain of the parent window:

The code is as follows: Copy code

AddEvent edit; // This is the pop-up window.

Edit = new AddEvent ();

Edit. eventype = 3; edit. AddAddEventEvent + = edit_AddAddEventEvent;

2) wpf route events:

The code is as follows: Copy code

Declare in the subwindow:

Public event Action <object, RoutedEventArgs> DetailMouseLeftUpDelegate;

Subwindow call:

Private void btndetail_Click (object sender, RoutedEventArgs e)
{
If (DetailMouseLeftUpDelegate! = Null & _ investManInfoBase! = Null)
{
DetailMouseLeftUpDelegate (_ investManInfoBase, e );
}
}
Parent window Event chain:

Card. DetailMouseLeftUpDelegate + = card_DetailMouseLeftUpDelegate;
Void card_DetailMouseLeftUpDelegate (object arg1, RoutedEventArgs arg2)
{
If (arg1! = Null)
{
}}


Note: The code intercepts the code for the project. May not be compiled. You can modify it yourself.

 

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.