简体   繁体   English

将Winform应用程序转换为WPF应用程序

[英]Convert a Winform application to WPF application

I have a little program which I made with WinForms and now I want to make it again using WPF. 我有一个用WinForms制作的小程序,现在我想用WPF重新制作它。

I'm new to WPF and I read that whatever you can do with XAML, you can also do without it, meaning using code only. 我是WPF的新手,我读到无论你用XAML做什么,你也可以没有它,这意味着只使用代码。

Of course that you don't have any XAML when using Winforms. 当然,使用Winforms时没有任何XAML。 Can I use the same code for the WPF application that I used for the winforms application and get the same result? 我可以对用于winforms应用程序的WPF应用程序使用相同的代码并获得相同的结果吗? Or do I need to create and edit XAML? 或者我是否需要创建和编辑XAML? What are the advantages of using or not using XAML? 使用或不使用XAML有什么好处?

Also, considering the past experience using Winforms, should I somehow change the way I'm thinking about design and implementation that worked for Winforms but are not that appropriate for WPF? 另外,考虑到使用Winforms的过去经验,我是否应该以某种方式改变我对Winforms的设计和实现的思考方式,但是不适合WPF?

No you can't reuse code from winforms in WPF. 不,你不能在WPF中重用winforms中的代码。

and even if you could, you shouldn't. 即使你可以,你也不应该。

whatever you can do with XAML, you can also do without it 无论你用XAML做什么,你也可以不用它

You should really use XAML to define the UI, and then use DataBinding and MVVM , which is a much more professional way of development than the traditional procedural winforms approach. 您应该使用XAML来定义UI,然后使用DataBindingMVVM ,这是一种比传统的程序winforms方法更加专业的开发方式。

Not using XAML is much more troublesome than using it. 不使用XAML比使用XAML麻烦得多。 It may look intimidating at first but it's a really awesome thing to work with. 一开始可能看起来令人生畏,但这是一件非常棒的事情。

Of course that you don't have any XAML when using Winforms 当然,使用Winforms时没有任何XAML

No, of course not. 不,当然不。 winforms is a really old technology that doesn't support anything. winforms是一项非常古老的技术,不支持任何东西。 That's why they created the Visual Studio designer, otherwise no one would have ever used winforms for anything, because of the horrendous gargantuan behemoth amount of code required to do anything useful. 这就是为什么他们创建Visual Studio设计器,否则没有人会曾经使用过winforms任何东西,因为做任何有用的东西需要巨大的巨大代码。

Can I use the same code for the WPF application that I used for the winforms application and get the same result? 我可以对用于winforms应用程序的WPF应用程序使用相同的代码并获得相同的结果吗?

Probably, by adapting some class names and whatnot, but then you lose the main advantage provided by WPF, which is precisely getting rid of the horrible winforms-like code. 也许,通过调整一些类名和诸如此类的东西,但是你失去了WPF提供的主要优势,这正是摆脱了可怕的类似winforms的代码。

considering the past experience using Winforms, should I somehow change the way I'm thinking about design and implementation that worked for Winforms but are not that appropriate for WPF? 考虑到过去使用Winforms的经验,我是否应该以某种方式改变我对Winforms的设计和实现的思考方式,但是不适合WPF?

Yes. 是。 WPF supports MVVM, and that requires a really different mentality from the traditional winforms approach. WPF支持MVVM,这需要与传统的winforms方法完全不同的心态。

I strongly recommend reading Rachel's Excellent Post about upgrading from winforms to WPF. 我强烈建议阅读Rachel的优秀帖子,关于从winforms升级到WPF。

I tried converting a Winforms app to WPF directly, and it causes way more issues than you need because you are fighting the framework all the time. 我尝试直接将Winforms应用程序转换为WPF,这会导致问题超出您的需要,因为您一直在与框架作斗争。 Read up on MVVM and databinding and use that. 阅读MVVM和数据绑定并使用它。 Its what WPF is designed around, and comes with several advantages such as testability. 它是WPF的设计,具有可测试性等几个优点。 You can actually get quite a long way with a few of the simple concepts (databinding, viewmodels etc...) and expand your knowledge as you go, but I'd recommend an understanding of MVVM and databinding in WPF first. 实际上,您可以通过一些简单的概念(数据绑定,视图模型等)获得相当长的路径,并随时扩展您的知识,但我建议首先了解MVF和数据绑定。

A good framework to get started with would be MVVMLight, but its worth writing the basics without a framework to get to know how things work first. 一个很好的入门框架将是MVVMLight,但它值得编写基础知识而不需要框架来了解事情的工作原理。

I seem to remember this being a good set of posts from reed copsey: http://reedcopsey.com/series/windows-forms-to-mvvm/ 我似乎记得这是来自芦苇copsey的一系列好帖子: http: //reedcopsey.com/series/windows-forms-to-mvvm/

Trying to create WPF controls without XAML is asking for trouble - the whole framework is built around the MVVM pattern which demands that your View be described in a declarative fashion, rather than a procedural fashion. 试图在没有XAML的情况下创建WPF控件会遇到麻烦 - 整个框架都是围绕MVVM模式构建的,这需要以声明的方式描述View,而不是程序化的方式。 While you can definitely create same UI objects in C# and XAML, doing so in C# will require you to know (in truly excruciating detail) how the framework operates in order to compensate when the state of your UI changes. 虽然你绝对可以在C#和XAML中创建相同的UI对象,但是在C#中这样做会要求你知道(真正令人难以理解的细节)框架的运作方式,以便在UI状态发生变化时进行补偿。 On the flip side, if you do it XAML (as WPF dictates you are supposed to), then things generally update just fine and without the necessity of working around endless bugs. 另一方面,如果你这样做XAML(正如WPF所规定的那样),那么事情通常会更新,并且无需解决无尽的错误。 In short "yes" you can do it in code, but "no" you can't do it in code without losing your sanity. 简而言之,“是”你可以在代码中完成它,但是“不”你不能在代码中做到而不会失去理智。

Actually it depends on how complex is your project. 实际上,这取决于你的项目有多复杂。 For quite a small project you can make this "conversion" quite simply. 对于相当小的项目,您可以非常简单地进行“转换”。 But it's not a good practice even because WPF development often is based on MVVM/Prism and they give you lots of advantages. 但这并不是一个好习惯,因为WPF开发通常基于MVVM / Prism,它们为您提供了很多优势。 Also the behavior of your UI might be different - it depends on the complexity of your project. 您的UI行为也可能不同 - 这取决于项目的复杂程度。

I'll 2nd or 3rd or 4th the other answers. 我将获得第2或第3或第4个其他答案。 Never, ever, ever, ever, ever try to build a WPF app with "Winforms style" code. 永远不会永远尝试用“Winforms风格”代码构建WPF应用程序。 I did that on my first ever WPF app and it was the biggest mistake I've ever made. 我在我的第一个WPF应用程序上做到了这一点,这是我做过的最大的错误。 What a gigantic mess. 真是一场巨大的混乱。 The 2nd app I went half-way towards MVVM and that too was a gigantic mess. 第二个应用程序我走向MVVM中途,这也是一个巨大的混乱。 3rd app and ever since I've gone 100% MVVM, DI, etc. and 100% by the book WPF and code is super clean. 第三个应用程序,自从我已经通过WPF和代码超级干净100%MVVM,DI等100%。

There isn't really a point in porting Winforms code to WPF any way. 将Winforms代码移植到WPF没有任何意义。 The majority of the code you wrote in Winforms is thrown out when you go WPF. 当您使用WPF时,您在Winforms中编写的大部分代码都会被抛弃。 Basically the only portable stuff is the business logic. 基本上唯一可移植的东西是业务逻辑。 All the UI logic is completely different. 所有UI逻辑都完全不同。

And you'll thank us later because one of the greatest concepts ever about WPF is the separation of UI and business logic. 之后你会感谢我们,因为WPF最伟大的概念之一是UI和业务逻辑的分离。 That makes swapping out controls a piece of cake. 这使控制换掉了一块蛋糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM