C# 将 Winform 应用程序转换为 WPF 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18968925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a Winform application to WPF application
提问by Yonatan Nir
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. Can I use the same code for the WPF application that I used for the winforms application and get the same result? Or do I need to create and edit XAML? What are the advantages of using or not using XAML?
当然,在使用 Winforms 时您没有任何 XAML。我可以对 WPF 应用程序使用与我用于 winforms 应用程序的代码相同的代码并获得相同的结果吗?还是我需要创建和编辑 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 的设计和实现的思考方式?
采纳答案by Federico Berasategui
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 DataBindingand MVVM, which is a much more professional way of development than the traditional procedural winforms approach.
你真的应该使用 XAML 来定义 UI,然后使用DataBinding和MVVM,这是一种比传统的程序化 winforms 方法更专业的开发方式。
Not using XAML is much more troublesome than using it. It may look intimidating at first but it's a really awesome thing to work with.
不使用 XAML 比使用它麻烦得多。乍一看可能令人生畏,但使用起来确实很棒。
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. 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.
不,当然不是。winforms 是一种非常古老的技术,不支持任何东西。这就是他们创建 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?
我可以对 WPF 应用程序使用与我用于 winforms 应用程序的代码相同的代码并获得相同的结果吗?
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 Postabout upgrading from winforms to WPF.
回答by Andrew
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. 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. 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. In short "yes" you can do it in code, but "no" you can't do it in code without losing your sanity.
尝试在没有 XAML 的情况下创建 WPF 控件是在自找麻烦 - 整个框架围绕 MVVM 模式构建,该模式要求以声明方式而不是过程方式描述您的视图。虽然您绝对可以在 C# 和 XAML 中创建相同的 UI 对象,但在 C# 中这样做将需要您了解(真正令人痛苦的细节)框架如何运行,以便在 UI 状态更改时进行补偿。另一方面,如果您使用 XAML(正如 WPF 规定您应该这样做的那样),那么事情通常会更新得很好,而无需解决无休止的错误。简而言之,“是”你可以用代码来做,但“不”你不能在不失去理智的情况下用代码来做。
回答by gmn
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. Read up on MVVM and databinding and use that. Its what WPF is designed around, and comes with several advantages such as testability. 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.
我尝试将 Winforms 应用程序直接转换为 WPF,但它会导致比您需要的更多的问题,因为您一直在与框架作斗争。阅读 MVVM 和数据绑定并使用它。WPF 就是围绕它设计的,并具有几个优点,例如可测试性。实际上,您可以通过一些简单的概念(数据绑定、视图模型等...)获得相当长的知识,并在进行过程中扩展您的知识,但我建议您首先了解 WPF 中的 MVVM 和数据绑定。
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/
我似乎记得这是 Reed copsey 的一组很好的帖子:http://reedcopsey.com/series/windows-forms-to-mvvm/
回答by Rinat Galyautdinov
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. Also the behavior of your UI might be different - it depends on the complexity of your project.
实际上,这取决于您的项目有多复杂。对于相当小的项目,您可以非常简单地进行这种“转换”。但这并不是一个好的做法,因为 WPF 开发通常基于 MVVM/Prism,并且它们为您提供了很多优势。此外,您的 UI 的行为可能会有所不同 - 这取决于您的项目的复杂性。
回答by SledgeHammer
I'll 2nd or 3rd or 4th the other answers. Never, ever, ever, ever, ever try to build a WPF app with "Winforms style" code. I did that on my first ever WPF app and it was the biggest mistake I've ever made. What a gigantic mess. The 2nd app I went half-way towards MVVM and that too was a gigantic mess. 3rd app and ever since I've gone 100% MVVM, DI, etc. and 100% by the book WPF and code is super clean.
我将是其他答案的第 2 个、第 3 个或第 4 个。永远,永远,永远,永远,永远不要尝试使用“Winforms 风格”代码构建 WPF 应用程序。我在我的第一个 WPF 应用程序上这样做了,这是我犯过的最大错误。多么巨大的混乱。我对 MVVM 进行了一半的第二个应用程序,这也是一个巨大的混乱。第三个应用程序,自从我 100% 使用 MVVM、DI 等并且 100% 使用 WPF 和代码非常干净。
There isn't really a point in porting Winforms code to WPF any way. The majority of the code you wrote in Winforms is thrown out when you go WPF. Basically the only portable stuff is the business logic. All the UI logic is completely different.
以任何方式将 Winforms 代码移植到 WPF 没有任何意义。当您使用 WPF 时,您在 Winforms 中编写的大部分代码都会被丢弃。基本上唯一可移植的东西是业务逻辑。所有的 UI 逻辑都完全不同。
And you'll thank us later because one of the greatest concepts ever about WPF is the separation of UI and business logic. That makes swapping out controls a piece of cake.
稍后您会感谢我们,因为 WPF 有史以来最伟大的概念之一就是 UI 和业务逻辑的分离。这使得交换控件变得轻而易举。