关于 MVVM 模式和 GUI 中 XAML 动态加载的混淆 [英] Confusion regarding MVVM pattern and dynamic loading of XAML in GUI

查看:66
本文介绍了关于 MVVM 模式和 GUI 中 XAML 动态加载的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,这个问题与 MVVM 模式有关,我可以在这个论坛上快速而准确地回答,所以我想问问并清除我对模式的困惑.

Well this question relates to MVVM pattern and i could good and fast answers on this forum so I thought to ask and clear the confusions i had about the pattern.

我对 MVVM 方法很陌生.我欣赏这种模式并理解其背后的原理.也许我对这种模式的研究不多,这就是为什么会有一些困惑.

I am quite new to MVVM approach. I appreciate the pattern and understand the principals behind it. Maybe I have not worked that much with the pattern that’s why there are a few confusions.

如果有一个场景,我想使用 XAML 动态加载 WPF 页面的几个部分,但仍然希望符合 MVVM 方法.

If there is a scenario in which I want to load few parts of my WPF page dynamically with XAML and still want to be compliant with MVVM approach.

困惑是:

  1. 使用 XAML 动态加载视图的逻辑所在.
  2. 我的 WPF 页面是否应该只有一个 ViewModel,或者每个单独的部分都有自己的视图模型,并与其他视图模型类进行交互.
  3. 如果我必须使用代码隐藏本身中的 C# 代码来构建显示在 GUI 上的控制树会怎样.对于使用代码创建的控件,我应该在视图本身的代码隐藏中执行命令绑定.

推荐答案

加载逻辑的去向并不是模式本身真正解决的问题.有一个有趣的博文 Ward Bell 关于这个.有很多方法可以给这只猫剥皮,而且它们都与 MVVM 兼容.我知道,这不是您真正要寻找的答案,但说实话:).查看 Ward 的博文……您将获得有关此主题的更深入讨论.

Where the logic for loading goes is something not really addressed by the pattern itself. There's an interesting blog post about this by Ward Bell. There's any number of ways to skin this cat, and they're all compatible with MVVM. Not really the answer you're looking for, I know, but it's honest :). Check out Ward's blog post... you'll get a much more in depth discussion of this topic.

至于页面是否有一个虚拟机,或者每个控件有一个虚拟机,这取决于.一般来说,我有一个页面.如果有一些部分可以在其他地方重用,我会将它分解为用户控件,并使用它自己的 VM,这意味着我们在 VM 中有一个 VM.我不同意rockeye的这一点.V-VM-M 之间没有一对一的关系.Youre 模型是根据业务需求设计的,完全不考虑演示.您的 ViewModels 是根据您的演示需求设计的,并且可能封装多个模型.事实上,它们封装很多模型是很常见的.

As for whether or not to have a single VM for the page, or one for each control, that just depends. Generally, I have one for the page. If there's some portion that reusable elsewhere, I break it out into a user control with it's own VM, which means we have a VM within a VM. I don't agree with rockeye on this one. There isn't a one-to-one relationship between V-VM-M. You're Models are designed according to business needs, with NO regard to presentation at all. You're ViewModels are designed according to your presentation needs, and may encapsulate more than one Model. In fact, it's very common for them to encapsulate many models.

像rockeye一样,我不明白你的最后一个问题.

Like rockeye, I don't understand your last question.

这篇关于关于 MVVM 模式和 GUI 中 XAML 动态加载的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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