【发布时间】:2017-09-19 12:53:29
【问题描述】:
有没有办法使用 JSON 架构创建 WPF UI?我知道可以在 AngularJS 和其他人的帮助下将其转换为 HTML 表单。但是寻找一种方法来创建 WPF 并没有取得成果。
存在一个Source by Rico Suter 关于如何创建 Visual Json 编辑器。我的要求与这里给出的略有不同。就我而言,我想基于架构和架构中提到的属性创建 WPF 控件。而且,在 UI 的帮助下,我希望能够通过在 UI 控件中输入值来创建尽可能多的 JSON 对象。
例如,让我们以下面的 JSON 架构为例。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {},
"id": "http://example.com/example.json",
"properties": {
"checked": {
"default": false,
"description": "An explanation about the purpose of this instance.",
"id": "/properties/checked",
"title": "The Checked Schema",
"type": "boolean"
},
"dimensions": {
"id": "/properties/dimensions",
"properties": {
"height": {
"default": 10,
"description": "An explanation about the purpose of this instance.",
"id": "/properties/dimensions/properties/height",
"title": "The Height Schema",
"type": "integer"
},
"width": {
"default": 5,
"description": "An explanation about the purpose of this instance.",
"id": "/properties/dimensions/properties/width",
"title": "The Width Schema",
"type": "integer"
}
},
"type": "object"
},
"id": {
"default": 1,
"description": "An explanation about the purpose of this instance.",
"id": "/properties/id",
"title": "The Id Schema",
"type": "integer"
},
"name": {
"default": "A green door",
"description": "An explanation about the purpose of this instance.",
"id": "/properties/name",
"title": "The Name Schema",
"type": "string"
},
"price": {
"default": 12.5,
"description": "An explanation about the purpose of this instance.",
"id": "/properties/price",
"title": "The Price Schema",
"type": "number"
},
"tags": {
"id": "/properties/tags",
"items": {
"default": "home",
"description": "An explanation about the purpose of this instance.",
"id": "/properties/tags/items",
"title": "The Empty Schema",
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
我希望能够为checked 属性显示一个复选框。同样,GroupBox 或带有 2 个TextBox 的控件可以输入尺寸(高度和宽度)。此 UI 应使用户能够输入所需的值,基于该值可以生成 JSON 对象。类似的,
{
"checked": false,
"dimensions": {
"width": 5,
"height": 10
},
"id": 1,
"name": "A green door",
"price": 12.5,
"tags": [
"home",
"green"
]
}
目前,我正在创建一个 JSchema 对象列表,并将每个属性反序列化为 JSchema 类型,然后将其添加到列表中。此后,我试图创建相同的控件。这只是一团糟,我还没有完全达到我的目标。然而,我不觉得我会对最终结果感到满意。如果您可以提出一种实现相同目标的方法,那将有很大帮助。谢谢。
样本取自
【问题讨论】:
-
“我希望能够为选中的属性显示一个复选框” - 好主意,你实现它的程度如何?
-
为什么不使用任何库将 json 数据转换为普通模型类,用于制作视图模型。例如Newtonsoft.json
-
这样做当然可能,但是没有内置任何东西,实现起来并不容易。基本思想是递归遍历 JSON,并以编程方式实例化控件,在它们上设置属性,然后将它们添加到当前的父项中。也可以通过创造性地使用 MVVM 和数据绑定来完成。
-
@MaheshMalpani 我没有任何 json 数据。我只有架构。
-
@BradleyUffner 目前,我正在递归地浏览 json 并创建控件。只是它使包括 UI 在内的所有内容都变得非常混乱。这就是为什么我需要知道是否已经存在任何东西。
标签: c# json wpf jsonschema