简体   繁体   English

是否可以在 .NET 中将 WinForm 转换为 WebForm?

[英]Is it possible to convert a WinForm to a WebForm in .NET?

I didn't think it was possible but I was just talking to a co-worker who said she had done it before.我不认为这是可能的,但我只是和一位同事交谈,她说她以前做过。 Is she pulling my chain?她在拉我的链子吗?

It's not something you can do automatically.这不是你可以自动完成的。

The trick is both Winforms and Webforms represent a form using a plain old class.诀窍是 Winforms 和 Webforms 都使用普通的旧类表示表单。 However, instances of the class in Webforms are short-lived compared to Winforms;然而,与 Winforms 相比,Webforms 中的类的实例是短暂的; each Webforms class instance represents only one HTTP request, rather than the entire lifetime of the form, as with Winforms.每个 Webforms 类实例仅表示一个 HTTP 请求,而不是像 Winforms 那样的表单的整个生命周期。 Every time you handle an event for your form in ASP.Net Webforms you're working with a brand new instance of the class.每次在 ASP.Net Webforms 中为表单处理事件时,您都在使用该类的一个全新实例。 Microsoft went to a lot of trouble to try cover for this issue as much as possible, but in the end it's just not a good idea to think of a Webform in the same terms as a Winform. Microsoft 费尽心思尽可能地解决这个问题,但最终将 Webform 视为与 Winform 相同的术语并不是一个好主意。

You can definitely take a Winforms app and rewrite it to use Webforms, but it will be just that: a rewrite.您绝对可以使用 Winforms 应用程序并重写它以使用 Webforms,但这只是:重写。

Converting a desktop application to a web application has several challenges:将桌面应用程序转换为 Web 应用程序有几个挑战:

  • Access to hardware访问硬件
  • Windows API calls Windows API 调用
  • Management of the application state应用状态管理
  • Access to the file system访问文件系统
  • Access control访问控制
  • Use of desktop-specific UI/UX/controls使用特定于桌面的 UI/UX/控件

There are options to convert desktop applications in an automated way, these can convert both the UI and the code of the application:有一些选项可以自动转换桌面应用程序,这些选项可以转换应用程序的 UI 和代码:

Even when using automated migration tools, in most of the cases you will have to perform a significant amount of manual work to get the application working in the same way as the original one.即使使用自动迁移工具,在大多数情况下,您也必须执行大量手动工作才能使应用程序以与原始应用程序相同的方式工作。

Some of these tools will help with different objectives, the first one will help you convert only the UI and to WebForms, the last two ones will generate ASP.NET MVC, one using a custom runtime and set of libraries and the other with common HTML/JS/CSS libraries such as Kendo MVVM, Kendo UI, AngularJS or Bootstrap among others.其中一些工具将有助于实现不同的目标,第一个将帮助您仅将 UI 转换为 WebForms,后两个将生成 ASP.NET MVC,一个使用自定义运行时和一组库,另一个使用通用 HTML /JS/CSS 库,例如 Kendo MVVM、Kendo UI、AngularJS 或 Bootstrap 等。 These tools will provide a solution that will be faster than writing the application in the web from scratch and will provide solutions or at least guidelines to approach the challenges mentioned before.这些工具将提供比从头开始在 Web 中编写应用程序更快的解决方案,并将提供解决方案或至少指南来应对前面提到的挑战。 However, there will be some differences from an application that was designed for the Web, simply because the architectures are different and usually the way of writing the code for a desktop application assumes thing that cannot be assumed for a web one.但是,与专为 Web 设计的应用程序会有一些差异,这仅仅是因为架构不同,并且通常为桌面应用程序编写代码的方式假设了 Web 应用程序无法假设的事情。

Disclaimer: I work for Mobilize.Net, who built WebMAP.免责声明:我为构建 WebMAP 的 Mobilize.Net 工作。

Yes, it is possible.对的,这是可能的。 If you have designed the application well enough it should be relatively easy to convert a Win Forms application to a web forms application by just swapping out the UI layer and replacing it.如果您的应用程序设计得足够好,只需换​​出 UI 层并替换它,就可以相对容易地将 Win Forms 应用程序转换为 Web 表单应用程序。 You are re-using the logic and data layers (which is where all the functionality would be).您正在重用逻辑层和数据层(这是所有功能所在的地方)。

Obviously, you have to write a new UI layer from scratch, but, if the logic layer is written well enough that isn't going to be too much work compared with writing the whole application again from scratch.显然,您必须从头开始编写一个新的 UI 层,但是,如果逻辑层编写得足够好,与从头开始重新编写整个应用程序相比,工作量不会太大。

However, there are some gotchas even if you have a very well written application.但是,即使您的应用程序编写得非常好,也有一些问题。 The logic layer may assume a stateful application in which case it may rely on lazy loading.逻辑层可能会假设一个有状态的应用程序,在这种情况下它可能依赖于延迟加载。 In a web application you have a stateless model which is run on requests and responses.在 Web 应用程序中,您有一个基于请求和响应运行的无状态模型。 In that scenario you'd know exactly what you need up front and can load just the bits you need and not other things that may be needed later on... because later on will be a different request/response cycle and all the data you collect now is going to be dropped as soon as the current response is completed.在那种情况下,您会预先确切地知道您需要什么,并且可以只加载您需要的位,而不是以后可能需要的其他东西……因为稍后将是不​​同的请求/响应周期和您需要的所有数据collect now 将在当前响应完成后立即删除。

I've recently been putting the logic of an application that was originally a WinForms into MVC and the biggest barrier to getting a responsive speed is the fact that, although reasonably well written, the logic layer assumed a stateful environment.我最近将最初是 WinForms 的应用程序的逻辑放入 MVC 中,获得响应速度的最大障碍是这样一个事实,尽管编写得相当好,但逻辑层假定了一个有状态的环境。 The same application is being also re-written for WPF (another stateful environment) without so many issues.同样的应用程序也正在为 WPF(另一个有状态环境)重新编写,没有这么多问题。

添加到以前的答案中,请注意,有一些 winform 功能在 webform 中根本不存在,因此它首先取决于 winform 中的确切内容。

just a comment more: the rewrite depends of how much logic is in the form itself.只是更多评论:重写取决于表单本身有多少逻辑。 With properly separated concerns, it is only a matter of plugging in another UI onto the business layer.通过正确分离关注点,只需将另一个 UI 插入业务层即可。

The problem is, offcourse, that 90% of apps isn't anything more that somewhat complex CRUD UI's (no real business logic)...问题是,当然,90% 的应用程序并不是那么复杂的 CRUD UI(没有真正的业务逻辑)......

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

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