C# 在 Web 浏览器中运行 WinForms 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17731744/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:20:47  来源:igfitidea点击:

Running a WinForms Application Inside Web Browser

c#wpfwebvisual-web-gui

提问by KF2

I have a win form application and i'm trying to run it inside the web browser,i rummaged through web and found some links:

我有一个 win 表单应用程序,我试图在网络浏览器中运行它,我翻遍了网络并找到了一些链接:

I tried this link:Embedding a .NET WinForms Application in an Internet Browser Using WPF,this works fine but the problem is that the end-user has to install dot net frame workfor running the application.

我尝试了这个链接:使用 WPF 在 Internet 浏览器中嵌入 .NET WinForms 应用程序,这工作正常,但问题是最终用户必须安装dot net frame work才能运行应用程序。

I found another link here for running a WinForms Application Inside Web Browser,with Visual WebGuidevelopment framework.

我在这里找到了另一个链接,用于使用 Visual WebGui开发框架在 Web 浏览器中运行 WinForms 应用程序

Has anyone had any experience with WebGui?

有没有人有使用 WebGui 的经验?

采纳答案by Federico Berasategui

winforms is not web. Web is Web.

winforms 不是网络。网络就是网络。

Web means HTML + something (usually JavaScript, CSS and probably jQuery or such).

Web 意味着 HTML + 某些东西(通常是 JavaScript、CSS 和可能的 jQuery 之类的)。

1 - If you need a web application, and want to do it with .Net, create an ASP.Net Application.

1 - 如果您需要一个 Web 应用程序,并希望使用 .Net 来实现,请创建一个 ASP.Net 应用程序。

2 - If you need a Windows application (Desktop), create a WPF application.

2 - 如果您需要 Windows 应用程序(桌面),请创建一个 WPF 应用程序。

3 - If you need a Windows 8 "Metro" style application (a.k.a Windows Store App), create a WinRT XAML application.

3 - 如果您需要 Windows 8“Metro”风格的应用程序(又名 Windows Store 应用程序),请创建一个 WinRT XAML 应用程序。

4 - winforms is completely useless. Forget that.

4 - winforms 完全没用。忘记吧。

If your customers don't want to install any version of the .Net Framework, your only option is #1.

如果您的客户不想安装任何版本的 .Net Framework,您唯一的选择是 #1。

A winforms application will NEVER be a web application, regardless of any hacks you do to make it run inside a web browser.

winforms 应用程序永远不会是一个 web 应用程序,无论你做了什么让它在 web 浏览器中运行的黑客。

an XBAP (WPF Browser Application) is also NOT a web application. It's just a regular WPF client application that does not have it's own window.

XBAP(WPF 浏览器应用程序)也不是 Web 应用程序。它只是一个普通的 WPF 客户端应用程序,没有自己的窗口。

Edit:

编辑:

If you did things the rightway (that is, maintaing separation of concerns and putting application logic in Controllers or ViewModels instead of the horrible code behind practices most winforms developers are generally used to), It should not be that hard to reuse your application logic and create a new application (web or otherwise) while maintaining the Data Access and Business Logic layers intact.

如果您以正确的方式做事(即,维护关注点分离并将应用程序逻辑放在控制器或视图模型中,而不是大多数 winforms 开发人员通常习惯的实践背后的可怕代码),那么重用您的应用程序逻辑应该不难并创建一个新的应用程序(Web 或其他),同时保持数据访问和业务逻辑层的完整性。

回答by Palli

Well, Visual WebGui isweb over ASP.NET. Its developing experience is the same/similar to that of Windows Forms using the Visual WebGui designer integrated into Visual Studio. You do your code behind just the same as you would on Windows Forms and the WebGui runtime transforms it into HTML5/CSS/JS.

好吧,Visual WebGui是基于ASP.NET 的网络。它的开发体验与使用集成到 Visual Studio 的 Visual WebGui 设计器的 Windows Forms 相同/相似。您可以像在 Windows 窗体上一样在背后处理代码,WebGui 运行时将其转换为 HTML5/CSS/JS。

The efforts needed to convert your Windows Forms application to a equivialend Visual WebGui web application will depend on the (3rd party / .NET) components used in your Windows Forms application. The Visual WebGui API is farily similar to that of Windows Forms, so in some cases you can add a few references to WebGui specific assemblies and then to a search/replace for a couple of namespaces. There are of course some differences, as these are two different platforms, but the developing experience is quite similar to Windows Forms.

将 Windows 窗体应用程序转换为等效的 Visual WebGui Web 应用程序所需的工作将取决于 Windows 窗体应用程序中使用的(第 3 方/.NET)组件。Visual WebGui API 与 Windows 窗体的 API 非常相似,因此在某些情况下,您可以添加一些对 WebGui 特定程序集的引用,然后添加对几个命名空间的搜索/替换。当然有一些差异,因为这是两个不同的平台,但开发体验与 Windows Forms 非常相似。

回答by MQuiggGeorgia

In my limited experience with Visual WebGui, it is pretty cool technology. In simple terms, as I understand it, you can use the Windows Forms development IDE to create an HTML5-compliant ASP.NET client/server web application... not just a web site or page, with a look and feel very much like a Windows forms desktop application. It does this by converting Winforms controls to jquery code on the client side, and stores most of the executable code on the server side.

在我对Visual WebGui 的有限经验中,它是一项非常酷的技术。简单来说,据我所知,您可以使用 Windows Forms 开发 IDE 创建一个符合 HTML5 的 ASP.NET 客户端/服务器 Web应用程序......不仅仅是一个网站或页面,外观和感觉非常像Windows 窗体桌面应用程序。它通过在客户端将 Winforms 控件转换为 jquery 代码来实现这一点,并将大部分可执行代码存储在服务器端。

I've used it to create simple applications for testing purposes, and there are a few caveats. First, to create a "pretty" website, you need to rethink the Winforms layout to make it web-like, with headers, sidebars, etc. It's a different paradigm, and users expect certain things on a website. The good news is that this adjustment can often be done fairly easy by simply manipulating controls; i.e. a Panel control can become a container for a sidebar or header. You can also create "themes", which are like skins for the controls to modify the appearance.

我已经使用它来创建用于测试目的的简单应用程序,但有一些注意事项。首先,要创建一个“漂亮”的网站,您需要重新考虑 Winforms 的布局,使其像 Web 一样,带有标题、侧边栏等。这是一种不同的范例,用户期望网站上有某些东西。好消息是,通过简单地操作控件,这种调整通常可以很容易地完成。即面板控件可以成为侧边栏或标题的容器。您还可以创建“主题”,它们就像用于修改外观的控件的皮肤。

Another consideration is that events for things like popup dialog boxes behave differently in a web environment. In Winforms program execution halts until the user clicks an "OK" button or something. Within Visual WebGui, all the execution happens on the server side; thus, program execution would continue without waiting for user input. This can be handled by creating a Form.Close handler for the popup box though. But these kinds of differences need to be kept in mind.

另一个考虑因素是弹出对话框之类的事件在 Web 环境中的行为不同。在 Winforms 中,程序执行会停止,直到用户单击“确定”按钮或其他东西。在 Visual WebGui 中,所有的执行都发生在服务器端;因此,程序将继续执行而无需等待用户输入。这可以通过为弹出框创建一个 Form.Close 处理程序来处理。但是需要牢记这些差异。

You also must consider object layout anomalies. Visual WebGui does a good job generally of mimicking the Winforms WYSIWYG benefit: Objects normally appear pretty close on the webpage to where you put them in the environment. But this isn't always the case; you need to play around with object padding, margins, etc. sometimes to get the right layout. Still it is often much easier than tediously changing spacing with CSS or div objects.

您还必须考虑对象布局异常。Visual WebGui 通常在模仿 Winforms 所见即所得的好处方面做得很好:对象通常在网页上与您将它们放在环境中的位置非常接近。但情况并非总是如此。您有时需要使用对象填充、边距等来获得正确的布局。尽管如此,它通常比用 CSS 或 div 对象繁琐地改变间距要容易得多。

回答by willvv

I think this question is very similar to this one:

我认为这个问题与这个问题非常相似:

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

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

As mentioned before, the only way to run the application "as is" inside a browser will be through the use of some activex control and that would require IE + .NET Framework. There are tools that you can use to automatically migrate the application to the Web, but there will always be some manual effort involved and it won't be the same as building the application from scratch as a web app.

如前所述,在浏览器中“按原样”运行应用程序的唯一方法是使用一些 activex 控件,这需要 IE + .NET Framework。您可以使用一些工具将应用程序自动迁移到 Web,但总会涉及一些手动工作,这与从头构建应用程序作为 Web 应用程序不同。