简体   繁体   English

如何在 WPF WebView2 中嵌入 Roomle Rubens 配置器?

[英]How can I embed the Roomle Rubens Configurator in a WPF WebView2?

I would like to embed the Roomle Rubens Configurator in a WPF dialog of a Windows desktop application.我想在 Windows 桌面应用程序的 WPF 对话框中嵌入Roomle Rubens 配置器。
I want to use a WebView2 control and have followed the instructions for Get started with WebView2 in WPF apps to create a desktop application.我想使用WebView2控件并按照WPF 应用程序中的 WebView2 入门指南创建桌面应用程序。
I also know how to embed the configurator in a website.我也知道如何在网站中嵌入配置器。 A nice documentation can be found here: Embedding Integration .可以在这里找到一个很好的文档:嵌入集成

My question is how do I put things together?我的问题是我如何把东西放在一起?

I would like to display the configurator as documented in the WebView of my application and communicate with it.我想显示我的应用程序的 WebView 中记录的配置器并与之通信。 I want to set and change parameters in my WPF UI, send the parameters to the web application and update the view.我想在 WPF UI 中设置和更改参数,将参数发送到 Web 应用程序并更新视图。

Do it step by step:一步一步来:

  1. Create a WPF dialog with a WebView2 control.使用 WebView2 控件创建 WPF 对话框。 See Get started with WebView2 in WPF apps请参阅WPF 应用程序中的 WebView2 入门

  2. Create a resource folder for the web content and add roomle-configurator-api.es.min.js .为 Web 内容创建一个资源文件夹并添加roomle-configurator-api.es.min.js The files can be found here: Roomle Rubens Configurator Embedding Lib .这些文件可以在这里找到: Roomle Rubens Configurator Embedding Lib
    (In the example the name of the folder is resource\\wwwroot ) (在示例中,文件夹的名称是resource\\wwwroot

  3. Get the quick start html from here: Embedding Integration - Copy & Paste without package manager .从这里获取快速入门html嵌入集成 - 没有包管理器的复制和粘贴
    Create an index.html file in the resource folder and paste the content from the quick start guide.在资源文件夹中创建一个index.html文件并粘贴快速入门指南中的内容。

  4. Establish a mapping between a virtual host name and the path of the web resources folder.在虚拟主机名和 Web 资源文件夹的路径之间建立映射。 Set the source of the web view by the local host:通过本地主机设置web视图的来源:

     async private Task InitializeAsync(WebView2 webView) { string hostName = "rubens.example" string localResourcePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, @"resource\\wwwroot"); await webView.EnsureCoreWebView2Async(); webView.CoreWebView2.SetVirtualHostNameToFolderMapping( hostName, localResourcePath, CoreWebView2HostResourceAccessKind.DenyCors); webView.Source = new Uri($"https://{hostName}/index.html"); }

    EnsureCoreWebView2Async wait for CoreWebView2 initialization. 确保CoreWebView2Async等待CoreWebView2初始化。 The method explicitly triggers initialization of the control's CoreWebView2.该方法显式触发控件的 CoreWebView2 的初始化。
    SetVirtualHostNameToFolderMapping sets a mapping between a virtual host name and a folder path to make available to web sites via that host name. SetVirtualHostNameToFolderMapping设置虚拟主机名和文件夹路径之间的映射,以便通过该主机名对网站可用。

  5. Add an event listener to the host app.向宿主应用程序添加事件侦听器。 In the following example, width and height data is retrieved from the event and set to the corresponding attributes of the root component with configurator.extended.setParameterOfRootComponent :在以下示例中,从事件中检索宽度高度数据并使用configurator.extended.setParameterOfRootComponent将其设置为根组件的相应属性:

     window.chrome.webview.addEventListener('message', arg => { if (arg.data.width) { configurator.extended.setParameterOfRootComponent({ "key": "width" }, arg.data.width.toString()); } if (arg.data.height) { configurator.extended.setParameterOfRootComponent({ "key": "height" }, arg.data.height.toString()); } });
  6. Set events to the host app:将事件设置到主机应用程序:

     public void ChangeHeight(WebView2 webView, int newHeight) => webView.CoreWebView2.PostWebMessageAsJson($"{{ \\"height\\" : {newHeight} }}");

    CoreWebView2.PostWebMessageAsJson posts the specified webMessageAsJson to the top level document in this WebView. CoreWebView2.PostWebMessageAsJson将指定的webMessageAsJson发布到此 WebView 中的顶级文档。
    CoreWebView2.PostWebMessageAsString posts a message that is a simple String rather than a JSON string representation of a JavaScript object. CoreWebView2.PostWebMessageAsString发布一条消息,它是一个简单的字符串,而不是 JavaScript 对象的 JSON 字符串表示。

The full example can be found in the GitHub repository: Roomle/roomle-configurator-wpf-cs-example完整的示例可以在 GitHub 存储库中找到: Roomle/roomle-configurator-wpf-cs-example

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

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