Saeid
Saeid

Reputation: 693

What += and -= do in this code?

I am new to wpf and mvvm concept. Here there is a tutorial I am studying but I cannot understand this part; In Figure 7:

 protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        MainWindow window = new MainWindow();

        // Create the ViewModel to which 
        // the main window binds.
        string path = "Data/customers.xml";
        var viewModel = new MainWindowViewModel(path);

        // When the ViewModel asks to be closed, 
        // close the window.
        EventHandler handler = null;
        handler = delegate
        {
            viewModel.RequestClose -= handler;
            window.Close();
        };
        viewModel.RequestClose += handler;

        // Allow all controls in the window to 
        // bind to the ViewModel by setting the 
        // DataContext, which propagates down 
        // the element tree.
        window.DataContext = viewModel;

        window.Show();
    }

What are viewModel.RequestClose -= handler; and viewModel.RequestClose += handler; doing?

Upvotes: 1

Views: 130

Answers (3)

Taha Paksu
Taha Paksu

Reputation: 15616

Think EventHandlers like a set of functions that'll be executed when an event occurs.

Market.SoldEvent += StockRemovalEvent;
Market.SoldEvent += PaymentReceivedEvent;

if(Market.SoldOut)
{
    // we don't need it anymore.
    Market.SoldEvent -= StockRemovalEvent;
}

Reference Link: https://msdn.microsoft.com/en-us/library/ms366768.aspx

Upvotes: 1

Seth Flowers
Seth Flowers

Reputation: 9190

viewModel.RequestClose += handler; adds the EventHandler to the RequestClose event. -= removes it.

Note that removing it is done as a cleanup, since it looks like the very next thing done in the handler is to close the window.

MainWindowViewModel is an object that publishes an event called RequestClose. Your code is subscribing to that event. Your code wants to handle when that event is fired. You do that by adding a handler to the event, using +=. When you do that, and the MainWindowViewModel instance fires the event, your handler runs. Events allow a sort of decoupled form of communication between objects. It looks like your handler is also going to close the window, so it takes further action to clean up by removing the handler from the event, with -=.

See the MSDN docs for Events.

Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.

Upvotes: 7

libertylocked
libertylocked

Reputation: 912

Subscribing and unsubscribing events.

viewModel.RequestClose += handler; means whenever RequestClose event is raised, the delegate defined in handler will be called.

viewModel.RequestClose -= handler unsubscribes it

See https://msdn.microsoft.com/en-us/library/ms366768.aspx#Anchor_0

Upvotes: 1

Related Questions