When to cancel events in Silverlight

One-line summary: . What is the best practice for untying event handlers created in the UserControl constructor in Silverlight2?

Background: I am currently creating an application for business applications in Silverlight2. Since Silverlight is a browser plugin, there is no Window concept - all this is done in UserControls. The way I handle various “forms” in the application is the top-level user control containing the viewport. To show different forms, I set the Child property from the viewport to different UserControls. My application has a singleton PageManager class that is called to open and close forms. Forms (UserControls) are stored on the stack. Opening the form places it on the top of the stack, closing it, removes it from the stack and shows it underneath.

I am trying to follow the Model-View-ViewModel pattern. In each form (derived from UserControl), I have a ViewModel that manages all the data for the view. ViewModel provides events, so the user interface can be notified of the completion of operations such as loading and saving.

In my form, I subscribe to an event in the constructor after I have a ViewModel

public partial class MyPage : UserControl
{

    public MyViewModel ViewModel{get; set;}

    // other constructors, which create the viewmodel and call the constructor below.

    public MyPage(MyViewModel viewModel)
    {
        InitializeComponent();
        ViewModel = viewModel;
        this.LayoutRoot.DataContext = this.ViewModel;

        // subscribe to event so we can do stuff
        this.ViewModel.LoadCompleted += new MyViewModel.LoadCompletedEventHandler(ViewModel_LoadCompleted);
    }

My question is: now that I have subscribed to this event, when do I delete the handler? Am I creating a destructor and doing it there, or does it create a chicken and egg situation when the garbage collector does not destroy the object until all the links (i.e., Event Handlers) have disappeared? Create an interface that forms must implement that defines the UnhookEvents function that is called when the form is closed using the PageManager?

: . , ViewModel , (UserControl)? , 95% . , , 2 , ViewModel. , , , ViewModel.

:

    private void AdvancedSessionSetupButton_Click(object sender, RoutedEventArgs e)
    {
        PageManager.GetPageManager().Close(this);
        PageManager.GetPageManager().Open(new CreateSessionPage(this.ViewModel), "Create Session");
    }

:

    private void BasicSessionSetupButton_Click(object sender, RoutedEventArgs e)
    {
        PageManager.GetPageManager().Close(this);
        PageManager.GetPageManager().Open(new CreateBasicSessionPage(this.ViewModel), "Create Session");
    }

PageManager.Close , , ViewModel. , , .

+5
2

, # Finalizer, . , ViewModel_LoadCompleted -, "this", ViewModel, "this". .

- .

, "this" ( ) - , , "this". , . , . .

, IDisposable unbind Dispose().

+2

, .

"= =" :

this.ViewModel.LoadCompleted -= ViewMode_LoadCompleted;

:

~MyPage
{
    this.ViewModel.LoadCompleted -= ViewMode_LoadCompleted;
}
+1

All Articles