开发者

How to Reset Silverlight View in ViewModel Without Creating New Instance

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 iss开发者_JS百科ues 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜