如何使用统一将相同的视图模型传递给子视图?

发布于 2024-07-19 19:57:29 字数 281 浏览 6 评论 0原文

我有一个业务对象,比如说客户,它有太多字段无法在一页中显示,因此我将其拆分到不同的选项卡页面。 数据无法以某种方式将其拆分为有意义的位(例如地址、州、街道名称等),因此我决定保留相同的模型和视图模型并具有不同的视图(每个选项卡页内容是不同的视图)绑定到相同的视图模型。

首先,这是正确的方法吗?为什么不呢?

其次,如果是,我如何使用统一将相同视图模型传递给子视图? 目前我使用构造函数注入,但它们是视图模型的新实例。

干杯,

阿里

I have a business object, lets say customer, it has too many fields to show in one page, so I'm splitting it to different tab pages. the data is not in a way that I can split it into meaningful bits (like address, having state, street name and so on), so I decided to keep the same model and view model and have different views (each tab page content is a different view) bind to the same view model.

first of all is this the correct approach and why not?

secondly, if it is, how do I use unity to pass the same view model to child views? at the moment I use constructor injection, but they are new instances of view model.

Cheers,

Ali

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

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

发布评论

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

评论(1

安静被遗忘 2024-07-26 19:57:29

如果您使用 Prism,则可以使用 RegionContext。

为 TabControl 指定 RegionContext:

<TabControl cal:RegionManager.RegionName="MyTabControl"
cal:RegionManager.RegionContext="{Binding Path=ViewModel}">

并使用 RegionContext 上的静态 GetObservableContext 方法从添加到该区域的视图访问它:

void ViewConstructor()
{
this.ViewModel = (MyViewModel)RegionContext.GetObservableContext(this).Value;
}

如果您没有使用 Prism,那么您可以将 ViewModel 注册为命名实例:

Container.RegisterInstance<IMyViewModel>("viewModelName", new MyViewModel());

并稍后使用以下方法获取它:

Container.Resolve<IMyViewModel>("viewModelName");

If you're using Prism, then you can use RegionContext.

Specify RegionContext for TabControl:

<TabControl cal:RegionManager.RegionName="MyTabControl"
cal:RegionManager.RegionContext="{Binding Path=ViewModel}">

And access it from Views added to this region using static GetObservableContext method on RegionContext:

void ViewConstructor()
{
this.ViewModel = (MyViewModel)RegionContext.GetObservableContext(this).Value;
}

If you are not using Prism, then you can register your ViewModel as a named instance:

Container.RegisterInstance<IMyViewModel>("viewModelName", new MyViewModel());

and get it later using:

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