MVVM - 我应该在哪里放置动态生成的代码并加载XAML? [英] MVVM - Where should I put code that dynamically generates and loads XAML?

查看:159
本文介绍了MVVM - 我应该在哪里放置动态生成的代码并加载XAML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是一个哲学问题,但是我有一些代码,通过在模型数据的运行时生成XAML并使用XamlReader加载XAML,通过在GridView中动态添加模板列。我的问题是,在MVVM世界中,我应该把代码完成这个?目前,我已经在代码隐藏了,但我正在考虑是否应该将其移动到ViewModel。

解决方案

WPF / Silverlight开发人员圈子中有一个很大的变化,可以转到MVVM架构解决方案,但是它们不像简单或者一般的例子那样进步。
这在简单的应用程序中不是什么大问题。当您考虑




  • 动态构建的屏幕或页面,或许是响应用户操作

  • 一个由
    区域组成的复合页​​面,包含视图,也许嵌套
    视图,每个视图实现为MVVP
    triads

  • 向导关闭
    个观看次数



这里有重要的逻辑。它在哪里去?



正是这个(你的问题)在哪里我得到挂起将我的东西移动到MVVM,我读了一篇很好的文章,给了我一种啊哈哈的时刻,这种架构适合一个更大的架构,其中一些类正在构成MVVM三元组的生命周期。



如果您想阅读完整的文章这里 Ward Bell gos更详细。
可能这只是更大的图片中的一个较小的部分,另一篇伟大的文章讨论了复合应用程序邻居的主要参与者,请参阅 here / p>

这与您的问题有什么关系?我的信念是ViewModel表示视图,并且您的视图是在运行时确定的,因此如果问题需要动态生成的列,那么它可能是创建渲染视图和适合您的视图模型的其他内容的责任最终结果和实例。


I guess this is sort of a philosophical question, but I have some code that dynamically adds templated columns to a GridView by generating the XAML at runtime from Model data and loading it using XamlReader. My question is, In an MVVM world, where should I put the code that accomplishes this? At the moment, I have it in the codebehind, but I'm contemplating whether I should move it to the ViewModel.

解决方案

There is a big movement in WPF/Silverlight developers circles to move to a MVVM architected solution however they dont go futher than simple or general examples. This isn’t much of an issue in simple applications. It becomes serious when you contemplate

  • a screen or page constructed dynamically, perhaps in response to user action
  • a composite page consisting of regions holding views, perhaps nested views, each implemented as MVVP triads
  • a wizard closing a collection of views with a single click

There is significant logic here. Where does it go?

Its exactly this (your question) where I get hangups moving my stuff to MVVM, Ive read a nice article that gave me kind of an ahha moment, this kind of architecture fits into a bigger one where some class is composing the lifecycle of the MVVM trio

If you want to read the full article here Ward Bell gos into greater detail. It may be that this is just a smaller piece in the bigger picture, another great article discusses the main players in the Composite Application Neighborhood , see here.

What does this all have to do with your problem? Well its my belief that the ViewModel represents the view, and your view is determined at runtime, so if the problem requires that there be dynamically generated columns it may be the responsiblity of something else to create the rendered view and the appropriate viewmodel that fits your final result and instance them.

这篇关于MVVM - 我应该在哪里放置动态生成的代码并加载XAML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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