在不同环境中将 UI 元素传入和传出 Windows 运行时组件? [英] Passing UI elements to and from Windows Runtime Component in different environments?

查看:26
本文介绍了在不同环境中将 UI 元素传入和传出 Windows 运行时组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以创建允许使用来自不同环境的 UI 元素的运行时组件(例如用 C# 编写).

I wonder if there is a way to create a runtime component (written in C# for example) which allows working with UI elements from different environments.

例如,我想将诸如 MediaElement 之类的 UI 元素附加到给定的 Panel (C#) 或 div (Javascript).

For example I would like to append an UI element such as MediaElement to a given Panel (C#) or div (Javascript).

我可以像这样在运行时组件中调用一个方法:cmpt.setPlayer((Panel) playerParent); (C#) 和 cmpt.setPlayer(getElementByClass('.playerParent')); (Javascript).这有可能吗?

Can I call a method in a runtime component like this: cmpt.setPlayer((Panel) playerParent); (C#) and cmpt.setPlayer(getElementByClass('.playerParent')); (Javascript). Is this possible somehow?

如果没有(我完全理解),有没有办法创建一个运行时组件,它提供了两种声明函数的方式,一种用于从 C# 调用,另一种用于Javascript?

If not (and I would totally understand that), is there a way to create one runtime component which offers let's say two ways of declaring a function, one for being called from C# and one for Javascript?

推荐答案

实际上不可能在 HTML/CSS 渲染引擎(JS 应用程序)和 C#/C++ 组件之间共享 UI 元素.C# 代码没有能够理解 HTML 元素对象的底层运行时.同样,来自 XAML 的 UIElement 对 JS 应用程序中的 HTML/CSS 引擎没有任何意义.简而言之,UI 元素无法跨该边界共享,因此 Windows 运行时组件适用于非 UI 功能.

It's not actually possible to share UI elements between the HTML/CSS rendering engine (the JS app) and a C#/C++ component. The C# code won't have the underlying runtime that understands an HTML element object. Similarly, a UIElement from XAML won't make any sense to the HTML/CSS engine in the JS app. In short, UI elements can't be shared across that boundary, and thus Windows Runtime Components are intended for non-UI functionality.

对于您的第二个问题,组件的结构不是按照您的建议执行的,即具有单独的 C# 和 JS 方法,因为您无法开始在 JavaScript 中实现组件.这个想法是用 C# 或 C++ 编写的方法由运行时投射到 JavaScript 中,但它仍然在这些语言中实现.

To your second question, components aren't structured to do what your suggest, i.e. having separate C# and JS methods, because you can't implement a component in JavaScript to begin with. The idea is that a method written in C# or C++ is projected into JavaScript by the runtime, but it's still implemented in those languages.

为 Windows 应用商店应用开发 UI 相关库的人通常为 C#/C++ 和 JS 实现不同的版本,因为这些差异.当然,非 UI 的东西可以进入它自己的 WinRT 组件,我认为你可以让一个 JS 库将东西附加到来自组件的命名空间,因为后者像其他任何东西一样作为 JS 对象投射到 JS 中.这种混合方法将使您的 API 表面区域保持在一个命名空间中,即使它具有混合实现.不过这个我没试过.

Folks who develop UI-related libraries for Windows Store apps generally implement separate versions for C#/C++ and for JS, because of these differences. Of course, non-UI stuff can go into its own WinRT component, and I think that you can have a JS library append stuff to a namespace coming from the component, because the latter is projected into JS as a JS object like any other. This kind of hybrid approach would keep your API surface area in one namespace, even though it has a mixed implementation. I haven't tried this though.

另一种方法可能是让 WinRT 组件使用各种回调来指示主机应用执行 UI 任务,这必然包括创建 UI 元素.组件可以管理参数和底层数据模型,即使用这些回调来相应地触发 UI 工作.

An alternate approach might be to have the WinRT component use various callbacks to instruct the host app to perform UI tasks, which would necessarily include creation of UI elements. The component could manage the parameters and underlying data model, that is, and use those callbacks to trigger the UI work accordingly.

值得一提的是,我在我的免费电子书 使用 HTML、CSS 和 JavaScript 编写 Windows 应用商店应用程序,第 2 版.

It's worth mentioning that I did a rather exhaustive treatment of WinRT components, including async, in Chapter 18 my free ebook, Programming Windows Store Apps with HTML, CSS, and JavaScript, 2nd Edition.

这篇关于在不同环境中将 UI 元素传入和传出 Windows 运行时组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆