.net MVVM应用程序和设计时数据 [英] .net MVVM application and design-time data

查看:133
本文介绍了.net MVVM应用程序和设计时数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




关于使用MVVM模式的WPF应用程序的设计时数据,我有一个更理论而不是动手的问题。



最近我在我的应用程序中使用了两种视图模型。在运行时使用的运行时视图模型和在设计时使用的设计时视图模型(当设计者在表达式混合中设计视图时)。我读到的有关此主题的所有内容都表明,设计时视图模型应该位于不同的程序集中,因此您不必将它们与应用程序一起发送。这就是我的问题所在。假设我有两个程序集:程序集XYZ.ViewModel(包含运行时视图模型)和XYZ.Design(包含设计时视图模型)。由于我经常使用IOC容器,因此同一视图的设计时和运行时视图模型必须实现一些共享接口。那么我应该在哪个程序集中放置接口?如果我把它们放在XYZ.Design程序集中,而不是XYZ.ViewModel必须引用该程序集,我必须使用appliation运送它。我试图将接口放在XYZ.ViewModel程序集中,但这会产生循环依赖性,因为XYZ.ViewModel程序集必须引用设计时视图模型的XYZ.Design程序集,XYZ.Design程序集必须引用XYZ .ViewModel汇编由于接口。



这个问题的唯一解决方案似乎是创建第三个程序集XYZ.Interfaces并将所有接口放在该程序集中。这种方法有效如果我使用IoC容器将设计时视图模型分配给视图,或者如果我在视图中使用以下表达式。

Hi
I have a more theoretical than a "hands on" question regarding the "design-time" data for WPF applicatons which use the MVVM pattern.

Lately I use two kinds of view-models in my applications. The "runtime" view-models which are used at runtime and "design-time" view-models which are used at design-time(when the designer is designing the views in expression blend). Everything I read about this topic suggests that the design-time view-models should be located in a different assembly so you don''t have to ship them with the application. And therein lies my problem. Let''s say I have two assemblies: Assembly XYZ.ViewModel(containing the runtime view-models) and XYZ.Design(containing the design-time view models). Since I oftenly use IOC containers both the design-time and runtime view-model for the same view must implement some shared interface. So in which assembly should I put the interfaces? If I put them in the XYZ.Design assembly than the XYZ.ViewModel must reference that assembly and I have to ship it with the appliation. I tried to put the interfaces in the XYZ.ViewModel assembly but that would create circual dependency becouse the XYZ.ViewModel assembly must reference the XYZ.Design assembly for the design-time view-models and the XYZ.Design assembly has to reference the XYZ.ViewModel assembly becouse of the interfaces.

The only solution to this problem seems to be to create the third assembly XYZ.Interfaces and put all the interfaces in that assembly. This approach works If I use the IoC container to assign the design-time view-model to the view or if I use the following expression in the view.

d:DataContext{d:DesignInstance, Type=design:SomeType, IsDesignTimeCreateble=True}





但在我看来,创建一个单独的程序集只是包含视图模型接口是很多的。如果你有多个aseemblies包含视图模型(让我们说不同的应用模块),你最终会得到三个组件。

所以我的问题是:有没有人找到更好或不同的方法来处理这个问题?任何关于这个主题的想法都将受到赞赏。


$ b $bUroš



But it seems to me that creating a separate assembly just to contain the view-model interfaces is to much. If you have multiple aseemblies containing view-models(let''s say for different applicaton moduls) than you end up with three assemblies for each one.
So my question is: Has anybody found a better or different approach to deal with this problem? Any thoughts on the subject will be appreciated.

Uroš

推荐答案

有趣的问题。



我不是一个很大的IOC用户,所以我提供的可能不适用。


$ m $ b in my mvvm' 'framework''我的设计时间数据(本质上是视图模型)继承自运行时视图模型。



我最初将它们放在带有视图的视图项目中 - 但是我只是快速尝试并将设计时间视图模型移动到他们自己的项目中,使用他们自己的命名空间,更改了视图中的引用,bob就像他们说的那样,我的叔叔。



我猜你也可以使用IOC,假设你在处理设计时间vm时可以参考具体的类(真正的VM)吗?



我的文章是这里 [ ^ ]

万一你想看看有什么我正在谈论!
Interesting question.

I''m not a big IOC user so what I offer may not be applicable.

in my mvvm ''framework'' my design time data (essentially view models) inherit from the run time view models.

I initially put them in the view project with the views - but I just had a quick try and moved the design time view models into their own project with their own namespace, changed the references in the views and bob was, as they say, my uncle.

I''d guess that using IOC this would be possible too assuming you can refer to a concrete class (the real VM) when you are dealing with the design time vm?

My articles are here[^]
in case you want to have look to see what I''m talking about!


嗨,,,

我支持你的上一个解决方案只是因为我认为这是你方案中最有效的解决方案。 />


我建议你给它命名像XYZ.Common(而不是XYZ.Interfaces)。我的意思是,这不仅仅是为了定义接口,而且也是其他类型,它们都是由ViewModels(设计和运行时)共同使用/共享。





如果您想进一步澄清,请告诉我。



最好的运气!!!
Hi,,,
I support your last solution only because I think thats the most effective one in your scenario.

I suggests you to give it a name something like XYZ.Common (instead of XYZ.Interfaces). By this I mean dont tie it only for defining Interfaces but also other types as well which are meant to be commonly used/shared by both ViewModels (Design & Runtime).


Do let me know if you want any further clarifications on this.

Best of LUCK!!!


这篇关于.net MVVM应用程序和设计时数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆