本文介绍了与d:DataContext的可混合性.我想念什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得仍然可以看到很多指导和建议,说明以视图为先的方法是在您的应用程序中获得可混合性的最佳方法.但是,使用d:DataContext,d:DesignData和d:DesignInstance,无论如何将视图和视图模型连接在一起,可混合性问题都不容易解决吗?

I feel as though I still see a lot of guidance and advice stating that a view-first approach is the best way to go to get Blendability in your application. However, with d:DataContext, d:DesignData and d:DesignInstance, isn't the problem of Blendability easy to solve regardless of how your views and viewmodels are wired together?

使用DesignInstance,您只需选择一个要在设计器中使用的具体ViewModel类型,就好像您具有视图优先的组合.实际上,与使用IsInDesignMode后空翻或创建默认构造函数(实际上只是为了在实际视图模型中提供设计支持)相比,使用DesignInstance实际上可以更好地分离关注点-您可以从视图模型类型或公共接口类型继承创建一个简单的设计者"视图模型,它完全约束了设计者.使用DesignData,您甚至不必这样做:您只需在XAML中以声明方式创建一个伪造的ViewModel.

With DesignInstance, you simply pick a concrete ViewModel type to be used in the designer, and it's as if you've got view-first composition. In fact, using DesignInstance actually allows for better separation of concerns than doing IsInDesignMode backflips or creating a default constructor just for design support in your real viewmodel - you can inherit from your viewmodel type or from a common interface type to create a simple "designer" viewmodel, and it's fully constrained to the designer. With DesignData, you don't even have to do that: you can just create a fake viewmodel declaratively in XAML.

是的,进行上述活动的前期成本很小,但实际上,与将设计数据与实际的视图模型混合在一起相比,结果实际上是更加清晰的分离.有什么我想念的吗?为什么可混合性仍然是一个大问题?

True, there is a small up-front cost in doing the above activities, but the result is actually a cleaner separation than what you'd get by mixing in design data with your real viewmodel. Is there something I'm missing? Why is Blendability still such a big concern?

推荐答案

如果您有完整的模型数据,则对于整个视图,使用d:DataContext,d:DesignData和d:DesignInstance应该可以使您具有可混合性.

If you have mockup data, for an entire view, then using d:DataContext,d:DesignData and d:DesignInstance should give you blendability.

因此,对于您的问题的答案是,简而言之,无论您如何在运行时使用视图(先查看视图还是先查看模型),都仍然可以实现可混合性.如果您的运行时版本中包含某些ViewModel逻辑或初始化,则应确保在设计时实例中包含相同的内容.

So the answer to your question is, in short, it doesn't matter how you use a view at runtime (view first or viewmodel first), you can still have blendablility. If you have some viewmodel logic or initialization in your runtime version, you should just make sure that the same is in your design-time instance.

许多人试图通过创建本质上两个视图模型来找到不需要重复工作的方式.我确实同意这样做也可以提供更好的分离效果.

Many people are trying to find a way that they don't need to duplicate work by creating essentially two viewmodels. I do agree that it does give better separation as well.

这篇关于与d:DataContext的可混合性.我想念什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 07:34