如何在 ViewModel 中重置 Silverlight 视图而不创建新实例

发布于 2024-08-16 21:37:24 字数 738 浏览 3 评论 0原文

在我的应用程序中,我在关联的视图模型的构造函数中实例化了视图的新实例。我还需要使用事件聚合器订阅一些事件。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宁愿没拥抱 2024-08-23 21:37:24

不是自动的。我认为您必须在视图中编写一个 Reset() 方法,在其中手动清除需要清除的内容。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文