Prevent simultaneous connection of MVVM ServiceAgent

Development of a Silverlight application that communicates with the WCF service.

MVVM → ServiceAgent → WCF Service

So, in my ViewModel model, I have:

ServiceAgent.SearchExternalPatients(Name, (s, e) => 
            { 
                ExternalPatients = e.Result;                    
            });

in my service agent i:

public void SearchExternalPatients(string name, EventHandler<SearchPatientExternalCompletedEventArgs> callback)
    {                          
        _proxy.SearchPatientExternalCompleted += callback;
        _proxy.SearchPatientExternalAsync(name);
    }

The problem is that every time I click the search button, it fires the event again, and when I get the result back, I get it several times.

What is the best way to undo these events in the MVVM ServiceAgent template?

+3
source share
2 answers

Do it like this:

public void SearchExternalPatients(string name, EventHandler<SearchPatientExternalCompletedEventArgs> callback)
{
    EventHandler<SearchPatientExternalCompletedEventArgs> wrapper = null;
    wrapper = (s, e) =>
    {
        callback(s, e);
        _proxy.SearchPatientExternalCompleted -= wrapper;
    }                          
    _proxy.SearchPatientExternalCompleted += wrapper;
    _proxy.SearchPatientExternalAsync(name);
}

ReSharper Visual Studio ( ), , , . , .

+2

Reactive Extensions ? Silverlight, RX , / . - .

+1

All Articles