WPF编程方法 [英] WPF Programming Methodology

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

问题描述

经过3个月的编程,我的APP在WPF上,我第二次想到了我编程我的应用程序的方式(我知道这可能是太晚了)。在我的APP上,我正在使用我的工具管理的软件API。我有DAL包含16个类,其中3个是单个。
我在 .cs 文件和 XAML 的课程中有一些逻辑。
我的问题是,我看到很多评论,WPF中写的应用程序应该使用MVVM,这将使代码更可用和可读,可以将我的代码转换为MVVM吗? MVVM的实际意义(不是维基百科还是手动定义)?



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



我知道我的问题是一个新的问题(我是新手:P)和一个抽象的问题,但我想知道我将写的应用程序将是最好的,我可以写这一次:)

解决方案

MVVM的实际含义是: UI不是数据。数据是数据,UI是UI



这意味着你不应该以程序逻辑(通常称为业务逻辑)紧密耦合或依赖于UI组件的状态,而是依赖于数据项的状态(无论是模型还是视图模型)。



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



这允许对UI(这是ViewModel)的抽象,所以如前所述,你的应用程序逻辑可以不依赖于UI,而是它的抽象。



这允许在UI和逻辑中有大量的可扩展性,并且还允许UI行为的几个方面的可测试性,因为大部分的UI行为在ViewModel中定义。



MVVM还有其他方面,但主要的实现就是这样。



编辑:



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



1 - 非MVVM WPF:



XAML:

 < StackPanel> 
< TextBox x:Name =txtLastName/>
< Button Content =点击我点击=Button_Click/>
< / StackPanel>

代码背后:

  private void Button_Click(object sender,EventArgs e)
{
//假设这是包含上述XAML的窗口后面的代码。
var lastname = this.txtLastName.Text;

//这里您可以从文本框中获取的数据执行一些操作
}

2 - MVVM WPF:



XAML:

 <&的StackPanel GT; 
< StackPanel.DataContext>
< my:MyViewModel />
< /StackPanel.DataContext>
< TextBox Text ={Binding LastName}/>
< Button Content =点击我Command ={Binding MyCommand}/>
< / StackPanel>

ViewModel:

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

public Command MyCommand {get;组; }

public MyViewModel()
{
//该命令在构造函数上接收一个动作
//这是在调用命令时执行的操作。
MyCommand = new Command(ExecuteMyCommand);
}

private void ExecuteMyCommand()
{
//仅为了说明的目的,不是真的需要。
var lastname = this.LastName;

//这里您可以从文本框中获取的数据执行一些操作
}
}

如上例所示,ViewModel根本不包含View的引用。因此,视图可以是任何东西,只要 {Bindings} 保持原位。



胶水这个魔术般地使他们一起工作是WPF UI Elements的 DataContext属性,它是所有绑定将被解析的对象。



还有其他一些事情,例如ViewModel中的属性更改通知启用双向绑定,但这不符合此答案的范围。



还要记住,MVVM是一种设计模式,而WPF是一个框架。 MVVM目前也被应用于其他技术(目前有关于MVVM的大量嗡嗡声,其中包括JavaScript和JavaScript等)。



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


After 3 months programming my APP on WPF, I've second thought about the way I'm programming my app (I know it's maybe too late). On 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. 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)?

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

I know my question is a bit newbie question (I'm newbie :P) and an abstract question but I want to know that the app I'll write will be the best that I can write at this time :)

解决方案

The actual meaning of MVVM is: UI is not Data. Data is Data, UI is 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).

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.

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.

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.

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

Edit:

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

1 - Non MVVM WPF:

XAML:

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

Code behind:

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>

ViewModel:

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
    }
}

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.

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.

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.

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)

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

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

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