MVVM 和自定义控件?

发布于 2024-12-08 16:24:55 字数 454 浏览 0 评论 0原文

我正在开发带有模块、MVVM 等的 PRISM 应用程序。我现在非常了解 PRISM 并且了解 MVVM 的价值。所有这些都有助于提供来自可测试性、“一致性”等的业务价值

但现在我遇到了某些交互问题。我已经花了很多时间尝试了解如何通过 MVVM 在 Silverlight 中设置焦点。所有这些附加行为、附加属性、触发器。这看起来像是一堆垃圾代码,而 MVVM 是根本原因。

例如,我需要创建查找控件,它基本上是带有按钮和弹出窗口的文本框。该控件本身需要大量的焦点控制,它需要将视图覆盖在父级(弹出窗口)上,等等。使用隐藏代码创建它似乎很容易,将其粘贴到单独的库中并继续。我的业务表单将在我漂亮的 MVVM PRISM 中使用此控件。

所以,问题是……在孤立的岛屿(如控件)中使用代码隐藏,并为带来业务价值的实际代码保留 MVVM 和 TDD 是否合理?

有没有一句“这里不会使用 MVVM”?

I'm working on PRISM application with modules, MVVM and so on. I understand PRISM pretty good now and I understand value of MVVM. All those things good to deliver business value which comes from testability, "uniformity" and so on.

But now I'm stuck with certain interaction issues. I already spent hours and hours trying to see how I set focus in Silverlight via MVVM. All this additional behaviors, attached properties, triggers. It just seems like bunch of junk code with MVVM being root cause.

For example, I need to create Lookup control which is basically textbox with button and popup window. This control itself needs lot of focus control, it needs to overlay view over parent (popups) and so on. It seems to be pretty easy to create it with code-behind, stick it into separate library and move on. My business forms will use this control inside my nice MVVM PRISM.

So, question is.. Is it justified to use code-behind in isolated islands like controls and keep MVVM and TDD for actual code that brings business value?

Is there line where you say "MVVM is not going to be used here" ?

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

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

发布评论

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

评论(1

ぺ禁宫浮华殁 2024-12-15 16:24:55

我认为使用代码隐藏绝对没有任何问题,前提是代码与视图特定的属性相关,例如设置焦点。您的 ViewModel 永远不需要知道或关心谁或什么拥有焦点,因为这是一个特定于视图的概念。

通常我以两种方式构建 UserControl:它们要么是为特定的 Model 或 ViewModel 构建的,要么是通用的,并且由调用它们的人提供它们的值。

对于前者,例如如果我想要一个 SearchResultsPopup,我会构建 UserControl,期望将 SearchResultsViewModel 之类的东西作为 DataContext。

例如,我的 UserControl 希望在其 DataContext 上找到以下属性,并在绑定中使用它们来构建视图。

  • ObservableCollection; Results
  • SearchResult SelectedResult
  • bool IsOpen
  • ICommand OkCommand
  • ICommand CancelCommand

然后我可以像这样使用 UserControl :

<local:SearchResultsPopup DataContext="{Binding MySearchResultsVM}" />

在后一种情况下,我要创建可供任何 Model 或 ViewModel 使用的通用内容,我将使用自定义依赖项属性为我的 UserControl 提供它需要绑定的值。

因此,在这个示例中,我将具有

  • bool IsOpen
  • ICommand OkCommand
  • ICommand CancelCommand

的 DependencyProperties并且我的 XAML 将如下所示:

<local:GenericPopup local:GenericPopup.IsOpen="{Binding IsPopupOpen}"
                    local:GenericPopup.SaveCommand="{Binding SavePopupCommand}"
                    local:GenericPopup.CancelCommand="{Binding HidePopupCommand}">

    <local:MySearchResultsView ... />
</local:GenericPopup>

总之,您的UserControl 要么是 ViewModel 的反映(意味着它成为一个视图),要么由视图提供值。 ViewModel 并不关心这两种情况。

I see absolutely nothing wrong with using Code Behind providing that the code is related to view-specific properties, such as setting Focus. Your ViewModel should never need to know about or care who or what has focus, since that is a View-Specific concept.

Usually I build UserControls in two ways: they are either built for a specific Model or ViewModel, or they are meant to be generic and have their values provided by whoever calls them.

In the case of the former, such as if I wanted a SearchResultsPopup, I would build the UserControl expecting to have something like a SearchResultsViewModel as the DataContext.

For example, my UserControl would expect to find the following properties on it's DataContext, and would use them in bindings to build the View.

  • ObservableCollection<SearchResult> Results
  • SearchResult SelectedResult
  • bool IsOpen
  • ICommand OkCommand
  • ICommand CancelCommand

I could then use the UserControl like this:

<local:SearchResultsPopup DataContext="{Binding MySearchResultsVM}" />

In the later situation, where I am creating something generic which can be used by any Model or ViewModel, I would use custom Dependency Properties to provide my UserControl with the values it needs to bind to.

So in this example, I would have DependencyProperties for

  • bool IsOpen
  • ICommand OkCommand
  • ICommand CancelCommand

And my XAML would look something like this:

<local:GenericPopup local:GenericPopup.IsOpen="{Binding IsPopupOpen}"
                    local:GenericPopup.SaveCommand="{Binding SavePopupCommand}"
                    local:GenericPopup.CancelCommand="{Binding HidePopupCommand}">

    <local:MySearchResultsView ... />
</local:GenericPopup>

In summary, your UserControl is either a reflection of your ViewModel (meaning it becomes a View), or it is provided values by the View. The ViewModel doesn't care either way.

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