与 d:DataContext 的可混合性。我缺少什么?

发布于 2024-09-16 17:16:24 字数 476 浏览 6 评论 0原文

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

使用 DesignInstance,您只需选择要在设计器中使用的具体 ViewModel 类型,就好像您已经获得了视图优先组合一样。事实上,使用 DesignInstance 实际上可以比 IsInDesignMode 后翻转或创建默认构造函数更好地分离关注点,以便在实际视图模型中提供设计支持 - 您可以从视图模型类型或通用接口类型继承创建一个简单的“设计者”视图模型,并且它完全受限于设计者。使用 DesignData,您甚至不必这样做:您只需在 XAML 中以声明方式创建一个假视图模型即可。

确实,执行上述活动需要很小的前期成本,但结果实际上是比将设计数据与真实视图模型混合所获得的分离更清晰。我有什么遗漏的吗?为什么可混合性仍然是一个大问题?

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?

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?

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

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

发布评论

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

评论(1

十秒萌定你 2024-09-23 17:16:24

如果您有整个视图的模型数据,那么使用 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.

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.

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