views:

192

answers:

1

In my application, I instantiate a new instance of my view in its associated viewmodel's constructor. I also have some events to subscribe to with an event aggregator.

public class FooViewModel
{
    private FooView TheView { get; set; }
    private IEventAggregator Aggregator { get; set; }

    public FooViewModel()
    {
        Aggregator = new EventAggregator();
        Aggregator.GetEvent<ListReceivedEvent>()
            .Subscribe(OnListReceived, ThreadOption.UIThread, true);
        TheView = new FooView();
    }

    public void OnListReceived(ObservableCollection<int> items)
    {
        TheView.Items = items;
    }
}

The problem with this is that if the view is manipulated and I reload it for a different type of Foo, then remnants of its previous use stick around. One way one can get around this is to instantiate a new view each time another context is needed, but this is causing issues with the RegionManager also being used in the application. Is there some way to reset a Silverlight View back to its initial state without having to instantiate a new instance of it?

A: 

Not automatically. I think you would have to write a Reset() method in the View where you manually clear whatever needs clearing.

Henrik Söderlund