WPF设计问题(自定义控件或mvvm) [英] WPF design question (custom control or mvvm)

查看:154
本文介绍了WPF设计问题(自定义控件或mvvm)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是场景:




  • 我有一个显示一些数据的图像

  • 视觉数据可以通过以下两种方式之一输入:


    1. 通过键盘或鼠标输入的用户

    2. 通过某些方式后端源


  • 这两个数据输入都可以是以下两种形式之一:


    1. 控制数据或

    2. 简单显示的原始数据


  • 控制数据会导致更改在视觉上

  • 原始数据只是按原样显示



由两个主服务器(即用户输入和后端输入)提供服务。
一个例子是一个多用户游戏,其视觉效果由用户输入
控制,但也可能具有由某些后端输入控制的视觉效果(例如tcp / ip)。
另一个例子是一个终端仿真器,它不仅可以获取用户输入,还可以从其他源(例如telnet或串行接口等)获取数据



I当时正在考虑为视觉效果编写WPF自定义控件。换句话说,
是一个黑盒子,它将解释输入并显示结果。
将用户输入此自定义控件很容易,因为可以监听
适当的事件并根据需要进行处理。但是,如何监听来自后端的
输入呢?公开一个绑定到的依赖属性不会使
有意义,但是在可视化对象上公开一个被数据调用的方法也不会使
有意义。



另一个选择是MVVM体系结构,其中Model是后端数据源
,而View Model可以完成所有工作。它同时获取后端数据(通过模型)
和用户输入(通过适当的命令绑定等),并使
具有适当的含义,并绑定到视图以显示这些更改。 / p>

自定义控件的优点在于,它可以作为
自己照顾自己的控件来使用,因此消费者只需要做很少的工作即可使用
,但问题是从后端获取数据。 MVVM方法是有利的,因为它巧妙地封装了处理逻辑,视图等。问题在于
这个模式必须为每个后端重复。因此,使视觉上的骨骼非常裸露
,并在控件之外公开所有处理逻辑。基本上,我希望
使其易于使用,以便有人可以使用它,而不必添加
太多的外部逻辑来进行处理等。他们提供的只是后端数据



很抱歉,这是一篇冗长的文章,但我正在学习WPF,这对我来说是一个有趣的设计
问题。欢迎所有想法,评论等。



感谢阅读。

解决方案

<我肯定会使用MVVM模式。您可以在代码中很好地分离关注点,并且还可以在用户界面外部测试视图模型。您也许还可以在Blend中编辑视图。我不认为将ViewModel连接到后端比连接自定义控件更复杂。您可能决定使用依赖项注入或服务定位器来连接事物。通过使用所有这些设计模式,您将获得一个更加分离和可测试的解决方案。


Here is the scenario:

  • I have a visual that displays some data
  • The data to the visual can come in one of two ways

    1. Via user input through the keyboard or mouse
    2. Via some backend source

  • Both these data inputs can be of one of two forms

    1. Control data or
    2. Raw data for simple display

  • Control data causes changes in the visual
  • Raw data is simply showed as is

In other words, the view is being served by two masters, viz user input and backend input. An example would be a multi-user game that has visuals controlled by user input but may also have the same visuals controlled by some backend input (say tcp/ip). Another example would be a terminal emulator that gets user inputs but also gets data from another source be it telnet or serial, etc.

I was thinking of writing a WPF custom control for the visual. In other words, it is a black box that will interpret the inputs and display the results. Getting user input into this custom control is easy since one can listen for the appropriate events and handle them as needed. However, how can one listen for the inputs from the backend? Exposing a dependency property that one binds to does not make sense but exposing a method on the visual that is called with the data also does not make sense.

Another choice is the MVVM architecture where the Model is the backend data source and the View Model does all the work. It gets both the backend data (via the model) and the user inputs (via appropriate command bindings or some such) and it makes appropriate sense of these and binds to the View to display these changes.

The advantage of the custom control is that it can be consumed as a control that takes care of itself so that the consumer has to do very little work to use it but the problem is getting data to it from the backend. The MVVM method is advantageous because it encapsulates the handling logic, view, etc neatly. The problem is that this pattern has to be repeated for every backend. Thus, making the visual very bare bones and exposing all the processing logic outside the control. Basically I want to make it very easy to consume so that someone can take it and use it without adding too much external logic to do processing etc. All they provide is their backend data source that feeds into the visual.

Sorry for this being a lengthy post but I am learning WPF and this is an interesting design question for me. All ideas, comments, etc welcome.

Thanks for reading.

解决方案

I would definitely use the MVVM pattern. You get a very nice separation of concerns in your code, and your viewmodel can also be tested outside of the user interface. You may also be able to edit you view in Blend. I don't think that hooking up the viewmodel to the backend is more complicated than hooking up a custom control. You may decide to use dependency injection or a service locator to connect things. By using all these design patterns you get a more decoupled and testable solution.

这篇关于WPF设计问题(自定义控件或mvvm)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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