WPF 编程方法论 [英] WPF Programming Methodology

查看:26
本文介绍了WPF 编程方法论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用的是我的工具正在管理的软件的 API.我有包含 16 个类的 DAL,其中 3 个是单例.我在 .cs 文件和 XAML 文件中有一些逻辑.

In my APP I'm using an API of a software that my tool is managing. I've DAL that contain 16 classes, 3 of them are singletons. I've some logic in the .cs files and XAML's off course.

我的问题是,我看到很多评论说用 WPF 编写的应用程序应该使用 MVVM,这将使代码更加可用和可读,我可以将我的代码转换为 MVVM 吗?MVVM 的实际含义是什么(不是维基百科或手动定义)?

My question is, I see a lot of comments that an app written in WPF should use MVVM, and this will make the code more usable and readable, can I transform my code to be MVVM? what it the actual meaning of MVVM (not Wikipedia or manual definition)?

我也使用 SQL 查询,我读过一篇关于 EF(实体框架)的论文,MVVM 和 EF 可以共存于同一个项目中吗?

I also use SQL queries and I read a paper about EF (Entity Framework), can MVVM and EF coexist together in the same project?

推荐答案

MVVM 的实际含义是:UI is not Data.数据就是数据,用户界面就是用户界面.

The actual meaning of MVVM is: UI is not Data. Data is Data, UI is UI.

这意味着您不应该以程序逻辑(通常称为业务逻辑)紧密耦合或依赖于 UI 组件状态的方式开发应用程序,而应使其依赖于数据项的状态(例如它是模型或视图模型).

This means that you should not develop the application in a way that the program logic (often called business logic) is tightly coupled or dependent on the state of UI components, but instead make it dependent on the state of data items (be it the Model, or the View Model).

例如,在其他框架(如winforms)中,如果您有一个包含文本框和按钮的屏幕,您通常会向按钮添加一个单击事件处理程序,然后从文本框中读取文本.在 MVVM 中,TextBox 的 Text 属性应该绑定到 ViewModel 中的 string 属性,按钮也应该绑定到 ViewModel 中的 Command.

For example, in other frameworks (such as winforms), if you have a screen that contains a textbox, and a button, you usually add a click event handler to the button and then read the text from the textbox. in MVVM, the Text property of the TextBox should be bound to a string property in the ViewModel, and the button should be bound to a Command in the ViewModel as well.

这允许对 UI(即 ViewModel)进行抽象,因此,正如我之前所说,您的应用程序逻辑可以不依赖于 UI,而是依赖于它的抽象.

This allows for an abstraction of the UI (which is the ViewModel), so that, as I said before, your application logic can be dependent on not the UI but an abstraction of it.

这允许 UI 和逻辑中的大量可伸缩性,并且还允许 UI 行为的多个方面的可测试性,因为 UI 行为的很大一部分是在 ViewModel 中定义的.

This allows for a huge amount of scalability in the UI and the logic, and also allows for the testability of several aspects of UI behavior because a big portion of the UI behavior is defined in the ViewModel.

MVVM 也有其他方面,但主要的实现是.

There are other aspects of MVVM as well, but the main realization is that.

为了答案的完整性,我将添加一个具体的例子:

I will add a concrete example of this for completeness of the answer:

1 - 非 MVVM WPF:

XAML:

<StackPanel>
   <TextBox x:Name="txtLastName"/>
   <Button Content="Click Me" Click="Button_Click"/>
</StackPanel>

背后的代码:

private void Button_Click(object sender, EventArgs e)
{
    //Assuming this is the code behind the window that contains the above XAML.
    var lastname = this.txtLastName.Text; 

    //Here you do some actions with the data obtained from the textbox
}

2 - MVVM WPF:

XAML:

<StackPanel>
   <StackPanel.DataContext>
       <my:MyViewModel/>
   </StackPanel.DataContext>
   <TextBox Text="{Binding LastName}"/>
   <Button Content="Click Me" Command="{Binding MyCommand}"/>
</StackPanel>

视图模型:

public class MyViewModel
{
    public string LastName { get; set; }

    public Command MyCommand { get; set; }

    public MyViewModel()
    {
        // The command receives an action on the constructor,
        // which is the action to execute when the command is invoked.
        MyCommand = new Command(ExecuteMyCommand); 
    }

    private void ExecuteMyCommand()
    {
        //Only for illustration purposes, not really needed.
        var lastname = this.LastName; 

        //Here you do some actions with the data obtained from the textbox
    }
}

正如您在上面的示例中看到的,ViewModel 根本不包含对 View 的引用.因此,只要 {Bindings} 保持在适当的位置,View 可以是任何东西.

As you can see in the above example, the ViewModel contains no reference at all to the View. Thus, the View could be anything, as long as the {Bindings} are kept in place.

神奇地使它们协同工作的粘合剂是 WPF UI 元素的 DataContext 属性,它是所有绑定将针对的对象进行解析.

The glue that magically makes them work together is the DataContext Property of WPF UI Elements, which is the object that all bindings will be resolved against.

还有其他内容,例如 ViewModel 中的属性更改通知以启用双向绑定,但这超出了本答案的范围.

There are other things, such as the Property Change Notification in the ViewModel to enable two-way bindings, but that is out of the scope of this answer.

还要记住,MVVM 是一种设计模式,而 WPF 是一种框架.MVVM 目前也被应用在其他技术中(目前有很多关于 MVVM 用于网络的讨论,包括 JavaScript 和类似的东西)

Also keep in mind that MVVM is a design pattern, whereas WPF is a framework. MVVM is also being currently applied in other technologies (there is currently a lot of buzz about MVVM for the web, with JavaScript and stuff like that)

我建议您阅读其他答案中提到的书籍以及本教程以了解更多 WPF 特定方面的内容.

I suggest you read the books mentioned in other answers as well as this Tutorial for more WPF-specific aspects.

这篇关于WPF 编程方法论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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