Removing Navigated event handler

9 views
Skip to first unread message

jas...@gmail.com

unread,
Feb 13, 2014, 11:12:04 PM2/13/14
to magellan...@googlegroups.com
In a WPF application, I am using this to created a handler in which I do some calculations etc on arrival at a view.
However, I find that the the handler is invoked on every navigation everywhere, and so I would like to remove the handler once
I no longer need it. I have tried -= and a utility found here but the hander remains and is still invoked.

In short, how can I removed my Navigated handler from Navigator?

Many thanks

James S

Caleb Vear

unread,
Feb 14, 2014, 7:52:44 AM2/14/14
to magellan...@googlegroups.com, magellan...@googlegroups.com
Hi James,

Can you post the code were you subscribe to the handle and where you are unsubscribing? It is really difficult to guess might be going wrong without seeing the code.

Cheers,

Caleb Vear

This message was sent from my mobile
--
You received this message because you are subscribed to the Google Groups "magellan-friends" group.
To unsubscribe from this group and stop receiving emails from it, send an email to magellan-frien...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

jas...@gmail.com

unread,
Feb 16, 2014, 9:38:32 AM2/16/14
to magellan...@googlegroups.com
Caleb:

The idea is simple - in an MVVM design, I want to know when I have navigated to a particular user control so that I can do some updating on arrival, and so I added the navigated handler in the ViewAttached override. The handler is in the same view model (VM).

I tried doing " -=" in a command handler in the same view model but this did not seem to work.

The code is trivial:

    protected override void ViewAttached(object view)
    {
      Navigator.Navigated += Navigator_Navigated;
    }

    public void CmdNextExecuted()
    {
      Navigator.Navigated -= Navigator_Navigated;
      // other code
    }

I am increasingly thinking that this design is bad/wrong and that I can not confine such an event handler to one VM the way I originally thought. For now, I have knowledge of the source of the navigation and so can run some conditional code in the hander and just put up with the navigated handler running all the time. I get the navigation info from the ExtraData string. As a supplementary question, I would like to know if it is possible to access the entire navigation history in this navigated handler, or anywhere else?

I am left wondering where, in general terms, it is best to put the navigated handler.

Thanks very much for your interest and help.

James.

Caleb Vear

unread,
Feb 16, 2014, 6:23:58 PM2/16/14
to magellan...@googlegroups.com

Hi James,

I think should should remove the handler from the Navigated event in the handler itself. Not in the handler for the command. The command won’t be executed until a button is clicked or something similar. Your code should look more like this:

protected override void ViewAttached(object view)
{
    Navigator.Navigated += Navigator_Navigated;
}

public void CmdNextExecuted()
{      
    // other code
}

private void Navigator_Navigated(object sender, EventArgs e)
{
    Navigator.Navigated -= Navigator_Navigated;
    // other code
}

Cheers,

Caleb

Reply all
Reply to author
Forward
0 new messages