Prism:如何在区域中注入视图模型实例?

发布于 2024-11-29 08:40:27 字数 518 浏览 2 评论 0原文

使用 Prism 几周后,我已经尝试过各种导航方法。但我仍然无法使用 Prism“开箱即用”实现一件事:导航到视图模型实例。导航到视图模型或视图类型确实很容易,但实际上没有办法导航到真正的视图模型实例

例如,当我有一系列不同的视图模型实例(也不同类型)时,如果选择了一个实例并且我想在我的内容区域中显示它,则我无法实现它,除非我通过事件聚合发布更改,并且我不这样做想要这样,因为这会让我在内容区域中陷入一种视图。我想要做的是将视图模型实例注册到区域中,以便视图可以从数据模板动态加载。

实际上,我通过创建 RegisterViewModelWithRegion 作为 RegionManager 的扩展方法来实现这一点,它工作得很好,但并不是很简洁,因为我必须手动查找我的应用程序资源,加载视图,附加我的视图模型实例,然后添加我所在地区的景色。

我真的觉得我在架构中遗漏了一些东西,因为这种做法对我来说似乎是显而易见的,但显然我找不到任何人有同样的担忧。

非常感谢您的帮助和经验。 谢谢。

After weeks of using Prism I have been through every kind of navigation methods. But there is still one thing that I haven't been able to achieve with Prism "out-of-the-box": navigate to a view-model instance. It is really easy to navigate to a view-model or view type but there is actually no way to navigate to a real view-model instance.

For example when I have a list of different view-model instances (different type too), if one is selected and I want to display it in my content region, I cannot achieve it unless I publish the change through event aggregation and I do not want that because this gets me stuck to one kind of view in my content region. What I want to do is to register a view-model instance into a region, so the view can load dynamically from data templates.

Actually I achieved to do this by creating a RegisterViewModelWithRegion as an extension method to RegionManager, it works well but it is not really neat as I have to manually lookup through my application resources, load the view, attach my view-model instance and then add the view to my region.

I really feel that I am missing something into the architecture because this kind of practice seem obvious to me but apparently I cannot find anybody having the same concern.

Your help and experience would much appreciated.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

赴月观长安 2024-12-06 08:40:27

经过与 Prism 团队的一些讨论,我发现实际上,使用 Region.Add(object view) 方法在某个区域中注入视图模型是完全可以的。它会给出完全相同的结果(除了一些小情况)。

我检查了 ItemsControlRegionAdapter ,它最终所做的只是

regionTarget.ItemsSource = region.Views;

regionTarget 作为目标控件(ListBox 例如)和 region.Views 属性是您注入区域的对象。

当然,感谢你的帮助雷切尔:-)

After some discussions with the Prism team, I found out that actually, using the Region.Add(object view) method to inject a view-model in a region is totally ok. It will give exactly the same result (except for some minor case).

I inspected the ItemsControlRegionAdapter and all it does in the end is

regionTarget.ItemsSource = region.Views;

regionTarget being the target control (ListBox for example) and region.Views property being the objects you inject into your regions.

Of course, thanks for your help Rachel :-)

霊感 2024-12-06 08:40:27

看看我写的关于使用导航的这篇文章 MVVM

您的应用程序 shell 需要一个 ViewModel,它定义哪个 ViewModelCurrentPage。要更改 ViewModel,您只需触发一个事件,shell 将拾取该事件以将 CurrentPage 设置为当前的 ViewModel。

您可以使用 PRISM 来切换页面,如下所示:

eventAggregator.GetEvent<ChangePageEvent>()
    .Publish(new ChangePageEventArgs(this));

我确信还有其他方法可以实现此目的,但到目前为止我发现这种方法是最简单的

Take a look at this article I wrote on using Navigation with MVVM

You need a ViewModel for your application shell that defines which ViewModel is the CurrentPage. To change ViewModels, you just fire an Event which the shell will pick up to set the CurrentPage to whatever ViewModel should be current.

You can switch pages with something like this with PRISM:

eventAggregator.GetEvent<ChangePageEvent>()
    .Publish(new ChangePageEventArgs(this));

I'm sure there's other ways to accomplish this, but so far I find this way the simplest

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