如何在 ViewModel 中重置 Silverlight 视图而不创建新实例
在我的应用程序中,我在关联的视图模型的构造函数中实例化了视图的新实例。我还需要使用事件聚合器订阅一些事件。
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;
}
}
这样做的问题是,如果视图被操纵并且我为不同类型的 Foo 重新加载它,那么它之前使用的残留物会保留下来。解决这个问题的一种方法是每次需要另一个上下文时实例化一个新视图,但这会导致应用程序中使用的 RegionManager 出现问题。有没有某种方法可以将 Silverlight 视图重置回其初始状态,而无需实例化它的新实例?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是自动的。我认为您必须在视图中编写一个 Reset() 方法,在其中手动清除需要清除的内容。
Not automatically. I think you would have to write a Reset() method in the View where you manually clear whatever needs clearing.