简体   繁体   English

在 C# 桌面应用程序中使用网页作为 UI?

[英]Use a webpage as the UI in a C# desktop application?

I'm building a C# desktop app with a simple UI.我正在构建一个带有简单 UI 的 C# 桌面应用程序。 Due to my familiarity with HTML/CSS, and a previous web-based iteration of a very similar app, it would be ideal if I could re-use some existing HTML/CSS pages I have as the UI for this new desktop app.由于我对 HTML/CSS 的熟悉,以及之前一个非常相似的应用程序的基于 Web 的迭代,如果我可以重用一些现有的 HTML/CSS 页面作为这个新桌面应用程序的 UI,那将是理想的。

Is that do-able with C#?这可以用 C# 实现吗? Can anyone show a simple example of using an HTML/CSS page as the UI of a C# desktop application?谁能展示一个使用 HTML/CSS 页面作为 C# 桌面应用程序 UI 的简单示例?

Other answers are dated, new good options:其他答案已过时,新的好选择:

  • Electron + Electron-Edge - is the way to go for a new Electron app that communicates to a .NET backend (a DLL). Electron + Electron-Edge - 是用于与 .NET 后端(DLL)通信的新 Electron 应用程序的方法。 Widely used and supported.被广泛使用和支持。
  • Neutronium - best for already-written .NET WPF apps that need a HTML UI. Neutronium - 最适合需要 HTML UI 的已编写的 .NET WPF 应用程序。 Requires using Vue.需要使用 Vue。 Less popular and less supported.不太受欢迎,支持较少。

CefSharp ( https://github.com/cefsharp/CefSharp ) is a good way to embed chromium, but you still need a web server. CefSharp ( https://github.com/cefsharp/CefSharp ) 是嵌入铬的好方法,但您仍然需要一个 Web 服务器。 Take a look at http://www.ultidev.com/products/Cassini/ to alternative for IIS.查看http://www.ultidev.com/products/Cassini/以替代 IIS。

You can use WebBrowser control to add Web browser functionality to your application.您可以使用WebBrowser控件向应用程序添加 Web 浏览器功能。

You can create your own html(s) and set your WebBrowser control to display those pages.您可以创建您自己的 html(s) 并设置您的WebBrowser控件以显示这些页面。 You can navigate by clicking hyperlinks or by using keyboard shortcuts to move backward and forward through navigation history.您可以通过单击超链接或使用键盘快捷键在导航历史记录中前后移动来导航。 By default, you can access additional browser functionality through the right-click shortcut menu默认情况下,您可以通过右键单击快捷菜单访问其他浏览器功能

You can even manage those pages centrally (for eg on LAN) and all clients on LAN can connect to this central location.您甚至可以集中管理这些页面(例如在 LAN 上),并且 LAN 上的所有客户端都可以连接到这个中央位置。 This way you can modify pages centrally.这样您就可以集中修改页面。

Check a walkthrough on MSDN:查看 MSDN 上的演练:

How to: Add Web Browser Capabilities to a Windows Forms Application如何:向 Windows 窗体应用程序添加 Web 浏览器功能

You can try to use a Chromium-based control - DotNetBrowser .您可以尝试使用基于 Chromium 的控件 - DotNetBrowser This control provides wide interactivity options, like DOM manipulation and DOM events, which can be useful in your case.此控件提供了广泛的交互选项,例如 DOM 操作和 DOM 事件,这在您的情况下很有用。

In addition, there is an article that demonstrates how to use a web page as HTML UI in a C# desktop application: https://www.infoq.com/articles/html-desktop-net此外,还有一篇文章演示了如何在 C# 桌面应用程序中使用网页作为 HTML UI: https : //www.infoq.com/articles/html-desktop-net

Affiliation disclosure: I work with the team, which created DotNetBrowser.隶属关系披露:我与创建 DotNetBrowser 的团队合作。

You could try adding a Web Browser control to a forms application to show your page.您可以尝试将 Web 浏览器控件添加到表单应用程序以显示您的页面。 But this will need to be remotely hosted as the code will not be able to execute locally, except if you listen for the specific links in c# and then perform the actions.但这需要远程托管,因为代码将无法在本地执行,除非您在 c# 中侦听特定链接然后执行操作。

You could also try your hand at WPF which is a new technology to create good looking gui's with mark-up much like html, which you'll pick up pretty quickly with your html experience.您还可以尝试使用 WPF,这是一种新技术,可以创建带有标记的漂亮 gui,就像 html 一样,您将通过 html 体验很快掌握。

Unfortunately neither WinForms or WPF is sufficiently like HTML/CSS to be of that kind of use.不幸的是,WinForms 或 WPF 都不够像 HTML/CSS 那样使用。 You'll be able to reuse the layout-concepts and art of course.当然,您将能够重用布局概念和艺术。 And, with WPF being the closest thing to it, you might even keep a lot of the code-behind code.而且,由于 WPF 是最接近它的东西,您甚至可能会保留很多代码隐藏代码。

But the short answer is: No但简短的回答是:不

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

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