简体   繁体   English

使用WPF实现WinForms?

[英]Implement WinForms using WPF?

This question is a result of a lunch-time conversation with a co-worker...I've read questions like WPF vs. Winforms ...and I personally think that long term WPF is the way to go. 这个问题是与同事进行午餐时间对话的结果......我读过像WPF和Winforms这样的问题......我个人认为长期 WPF是要走的路。 The problem/question is what to do in the meantime. 问题/问题是在此期间要做什么。

Yes, WPF certainly has its advantages; 是的,WPF当然有其优势; not being built on GDI/USER is one of them. 不是建立在GDI / USER上的就是其中之一。 But at this point in time (ie, late 2009; using VS2008 or perhaps even VS2005; Silverlight3 recently released and not yet widely adopted/deployed), WPF almost looks like it might be an "over engineered" solution. 但是在这个时间点(即2009年末;使用VS2008或甚至VS2005; Silverlight3最近发布并且尚未广泛采用/部署),WPF几乎看起来可能是一个“过度设计”的解决方案。 While I'm sure that will change with time, it doesn't make things any easier today, nor for the immediate (say <36 mo) future. 虽然我确信它会随着时间的推移而改变,但它不会让今天的事情变得更容易,也不会使眼前的(比如<36个月)更加容易。

And let's face it, WinForms is really simple and easy; 让我们面对现实,WinForms 非常简单易行; especially for the many co-workers who are still "happily" using MFC. 特别是对于仍然“幸福地”使用MFC的许多同事而言。 Yes, it may be difficult to do slick animation, 3D graphics, gradients, etc.; 是的,可能很难做出光滑的动画,3D图形,渐变等; but it's a very utilitarian solution that a lot of people (ie, C++/MFC developers) readily understand today . 但它是一个非常实用的解决方案,很多人(即C ++ / MFC开发人员) 今天很容易理解。

With that longish introduction--has anybody thought/tired/etc. 随着那个漫长的介绍 - 有人想/累了/等等。 about the idea of implementing (most of) WinForms using WPF (ie, WinForms sans GDI/USER)? 关于使用WPF实现(大多数)WinForms的想法(即,WinForms 没有 GDI / USER)? I'm sure given things like Control.Handle , 100% re-implementation isn't possible. 我确定给出像Control.Handle这样的东西,100%重新实现是不可能的。 But it certainly seems like many WinForms controls could be re-implemented using WPF "under the hood". 但看起来很多WinForms控件似乎可以使用WPF“引擎盖下”重新实现。 Or is this really border-line impossible? 或者这真的是边界线不可能吗?

By "re-implement", I envision removing assembly references to System.Windows.Forms replacing them with (say) Microsoft.Wpf.WinForms and then rebuilding my application. 通过“重新实现”,我设想删除对System.Windows.Forms的程序集引用,用(比如) Microsoft.Wpf.WinForms替换它们,然后重建我的应用程序。 After that I would expect to fix some (relatively small number of) compiler and/or runtime errors (say P/Invokes to Win32 APIs). 之后,我希望修复一些(相对较少数量的)编译器和/或运行时错误(比如P / Invokes到Win32 API)。

Something like this seems like a nice complement to Microsoft's various WinForms/WPF interop strategies such as WindowsFormsHost . 像这样的东西似乎是微软各种​​WinForms / WPF互操作策略(如WindowsFormsHost)的很好的补充。 For example, developers could start to use/learn WPF in a much more incremental fashion. 例如,开发人员可以以更加渐进的方式开始使用/学习WPF。

Edit: While the various "why?" 编辑:虽然各种“为什么?” discussions are interesting, they don't answer the basic technical question: "yes., ..here's how....; or no, ...because...". 讨论很有意思,他们没有回答基本的技术问题:“是的,......这是怎样的......;或者不,......因为......”。

Honestly, I think you could probably implement a layer to allow you to port most of WinForms to WPF, but you really shouldn't. 老实说,我认为你可能实现一个层,允许你将大多数WinForms移植到WPF,但你真的不应该。 Let me use an analogy - Moving from WinForms to WPF is like moving from an imperative language to an object oriented one (or from OO to pure functional). 让我用一个类比 - 从WinForms转移到WPF就像从命令式语言转向面向对象语言(或从OO转向纯函数)。 It's really hard, nothing seems to work right, solutions don't make sense, everything seems over-engineered, and then... you get it . 它真的很难,似乎没有什么工作正常,解决方案没有意义,一切似乎都过度设计,然后......你明白了 You have a flash of insight, the light bulb goes on and you see a whole new world where tasks are easier to accomplish and easier to maintain (than in WinForms). 你有一个洞察力,灯泡继续,你看到一个全新的世界,任务更容易完成,更容易维护(比在WinForms中)。

But first you have to grok it. 但首先你必须弄清楚它。 It's really easy to keep coding in the WinForms style in WPF so you have to force yourself not to or you'll never take that next step. 在WPF中保持WinForms风格的编码非常容易,所以你必须强迫自己不要或者你永远不会采取下一步。 Force yourself to start from a clean slate, learn how to apply MVVM, take a look at Prism, build some really simple apps and then add to them to explore how to manage complexity the WPF way. 强迫自己从一个干净的平板开始,学习如何应用MVVM,看看Prism,构建一些非常简单的应用程序,然后添加它们以探索如何管理WPF方式的复杂性。 You might implement WinForms in WPF but all you would ever have is people coding WinForms in WPF. 您可以在WPF中实现WinForms,但您所拥有的只是在WPF中编写WinForms的人。 You basically end up with the equivalent of (from the Imperative -> OO days) making every class a singleton with all static methods. 你基本上最终得到了(从Imperative - > OO天),使每个类都成为一个包含所有静态方法的单例。 You might be "using" WPF, but you (and/or your coworkers) will never grow past that stage. 您可能正在“使用”WPF,但您(和/或您的同事)将永远不会超过该阶段。

From what I can glean from your situation I would recommend that you not try to switch to WPF unless you are writing a new application with a small team. 根据我的情况,我建议您不要尝试切换到WPF,除非您正在与一个小团队一起编写新的应用程序。 If that does end up being the case then the small team can be a nucleus for change because once one person in your organization "gets it" they will teach others near them until the light bulbs start turning on all around over the place. 如果情况确实如此,那么小团队就可以成为改变的核心,因为一旦你的组织中的一个人“得到它”,他们就会教他们附近的其他人,直到灯泡开始四处转动。 That's what we've done in my current organization and looking back the progress everyone's made just in the last 6 months is staggering. 这就是我们在现有组织中所做的,回顾过去6个月中每个人所取得的进步是惊人的。

I'm not really sure I understand your question - it is already possible to use WPF libraries in a Winforms like fashion (assemble everything in code with no XAML), and most of the common Winforms controls have their direct WPF analogs. 我不太确定我理解你的问题 - 已经可以像Winforms一样使用WPF库(在没有XAML的情况下组装所有代码),并且大多数常见的Winforms控件都有它们的直接WPF模拟。

The real question is why would you want to? 真正的问题是你为什么要这样做? Many of the big challenges of developing Winform applications are easily solved with WPF databinding and templating techniques. 使用WPF数据绑定和模板技术可以轻松解决开发Winform应用程序的许多重大挑战。

And I'm not even talking about fancy visual effects, which just take waaaaaaaay too much work in winforms, but simply something like visualizing a list of arbitrary items in a collection. 而且我甚至都没有谈论花哨的视觉效果,它只是在winforms中花费太多的工作,而只是想象一下集合中任意项目的列表。

Furthermore, if you assemble everything in code Winforms style you don't get the design time editing support. 此外,如果您以代码Winforms样式组装所有内容,则无法获得设计时编辑支持。

First off, WPF isn't "over engineered". 首先,WPF并非“过度设计”。 I would say it is just perfectly engineered, as it is an extremely complex endevor to implement a UI framework that supports rich composability, rich databinding, 2D and 3D in a single platform. 我认为它只是完美的设计,因为在一个平台上实现支持丰富的可组合性,丰富的数据绑定,2D和3D的UI框架是一项非常复杂的工作。

WPF has a slight learning curve, but it isn't very steep, and occurs in stages. WPF有一个轻微的学习曲线,但它不是很陡峭,并且分阶段发生。 Give yourself 2 days with it, and you will NEVER look back. 给自己2天,你永远不会回头。 WinForms is easy for easy things. WinForms很容易实现。 WPF is easy for just about anything, and the capabilities you have via its extensive composability and databinding will put WinForms to shame for anything more complex than your most basic app. 几乎任何东西都可以轻松使用WPF,并且通过其广泛的可组合性和数据绑定所拥有的功能将使WinForms感到羞耻,因为它比最基本的应用程序更复杂。

As for WinForms controls being reimplemented...there is little need. 至于重新实现的WinForms控件......几乎没有必要。 A lot of the highly complex WinForms controls that are available from third parties are only required because of how difficult it is to customize the UI of any WinForms control. 由于自定义任何WinForms控件的UI有多困难,因此只需要第三方提供的许多高度复杂的WinForms控件。 With WPF, enhancing any OOB control is easy, and nearly limitless. 使用WPF,增强任何OOB控制都很容易,几乎是无限的。

I highly recommend WPF as the UI platform of choice, unless your target hardware is incapable of rendering WPF. 我强烈推荐WPF作为首选的UI平台,除非您的目标硬件无法呈现WPF。

An analogy for your coworkers: 你的同事的类比:

Which is better, the automobile or the horse and buggy? 哪个更好,汽车还是马和马车?

  1. Automobiles are not as mature a technology as the horse and buggy. 汽车并不像马和马车那样成熟。 Horse and buggy has been around over 2000 years, automobiles less than 200. 马和越野车已经有2000多年了,汽车不到200辆。

  2. Automobiles are overly engineered and extremely complex. 汽车设计过度,非常复杂。 Horse and buggy is simple. 马和越野车很简单。

  3. People in many parts of the world don't know how to drive an automobile but have good skills with the horse and buggy. 世界上许多地方的人都不知道如何驾驶汽车,但是他们拥有马匹和马车的良好技能。

Question: Can we simplify our automobiles so they are controlled by reins, eat hay, and be repaired by veterinarians and carriage-makers? 问题:我们能否简化我们的汽车,以便他们受到缰绳的控制,吃干草,并由兽医和马车制造商修理?

My point here is that WPF is a very mature technology by now, much more powerful and easy to use than WinForms, and isn't at all difficult to learn. 我的观点是,WPF是一种非常成熟的技术,比WinForms更强大,更易于使用,并且一点都不难学。 It should be the vehicle of choice except for "country drives" and other specialized purposes. 它应该是选择的载体,除了“乡村驱动器”和其他专门用途。

I agree with the others that it is trivial to use WPF in a WinForms-like manner so there need be no significant learning curve if you want to use it that way, but it is wasteful not to take advantage of the new paradigms. 我同意其他人认为以类似WinForms的方式使用WPF是微不足道的,所以如果你想以这种方式使用它,就不需要有重要的学习曲线,但是不利用新的范例是浪费的。

I disagree that you should delay implementing WPF in your main application. 我不同意您应该延迟在主应用程序中实现WPF。 It's very easy and painless to mix WinForms and WPF content in a single application. 在单个应用程序中混合WinForms和WPF内容非常容易和轻松。 I recommend building new pieces in WPF and leaving old pieces in WinForms until there is a need to touch something. 我建议在WPF中构建新的部分,并在WinForms中留下旧部分,直到需要触摸某些内容。 Even if your existing application is native C++, it is still easy to use WPF with it as long as the data is easily accessible from managed code. 即使您现有的应用程序是本机C ++,只要可以从托管代码轻松访问数据,使用WPF仍然很容易。

A thing called the WindowsFormsHost can help out with moving controls over initially. 一个叫做WindowsFormsHost的东西可以帮助你最初移动控件。 However, I've found it to be pretty slow. 但是,我发现它很慢。 You're much better off spending the time to learn WPF and write everything from scratch and take advantage of the features, especially data binding! 你最好花时间学习WPF并从头开始编写所有内容并利用这些功能,尤其是数据绑定!

You really must look at it from the long term perspective of the life of the application you are creating. 你真的必须从你正在创建的应用程序的生命周期的长远角度来看待它。 While WPF might not be as mature as Windows Forms, it allows a programmer to separate design (XAML) and logic (C#/VB). 虽然WPF可能不像Windows Forms那样成熟,但它允许程序员将设计(XAML)和逻辑(C#/ VB)分开。 I do not believe it would be good if you create your controls in WPF in a Windows Forms style. 我不相信如果你在Windows窗体样式的WPF中创建控件会更好。 Imagine placing it all back in XAML for some reason in the future! 想象一下,将来由于某种原因将它全部放回到XAML中!

That being said, if you really wish to bring some Windows Forms' controls into WPF it is entirely possible. 话虽这么说,如果你真的希望将一些Windows Forms的控件带入WPF,那是完全可能的。 You simply need to add a reference to WindowsFormsIntegration, add the appropriate namespaces for it in your code and a add a WindowsFormsHost tag in your XAML. 您只需添加对WindowsFormsIntegration的引用,在代码中为其添加适当的命名空间,并在XAML中添加WindowsFormsHost标记。 In my opinion, using this should only be temporary (it is rather slow for the moment). 在我看来,使用它应该只是暂时的(目前它相当缓慢)。 On the other hand, future frameworks will most likely make WPF a lot better (having 4.0 in my mind). 另一方面,未来的框架很可能会使WPF变得更好(我脑子里有4.0)。

Learning WPF now is probably the best thing you can do. 现在学习WPF可能是你能做的最好的事情。 Getting to know your way around and using the WPF Toolkit for certain missing controls from Windows Forms can be a bit time consuming at first since there are some things that are different from Windows Forms (control customizing and data binding comes to mind). 通过了解自己的方式并使用WPF Toolkit从Windows窗体中获取某些缺少的控件起初可能会耗费一些时间,因为有些东西与Windows窗体不同(控件自定义和数据绑定会浮现在脑海中)。

If you keep on using Windows Forms you will be missing out on new features in WPF that make development faster. 如果继续使用Windows窗体,您将错过WPF中的新功能,从而加快开发速度。 For example, you can have people working on the design while others work on the logic side of the application because of the more pronounced separation of design and logic. 例如,您可以让人们从事设计工作,而其他人则在应用程序的逻辑方面工作,因为设计和逻辑的分离更为明显。

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

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