【问题标题】:Generate C# code from XAML从 XAML 生成 C# 代码
【发布时间】:2014-03-11 06:47:32
【问题描述】:

在我当前的项目中,我已经在 C# 中动态地制作 UI。这可能是一项非常无聊和困难的任务。那么有没有可以生成 XAML UI 标记的 C# 代码的工具呢?

例如,我在 XAML 中有网格

    <Grid  >
        <Grid.RowDefinitions>
            <RowDefinition  />
            <RowDefinition  />
        </Grid.RowDefinitions>
    </Grid>

我希望将其转换为

 Grid grdTextField = new Grid();
 RowDefinition firstrow = new RowDefinition();
 RowDefinition secondrow = new RowDefinition();

这是一个简单的案例,但对于稍微复杂的 UI,很难从 Code behind 中制作出来。

编辑:-

实际上,我得到了一个 Json,它决定了我的大型网格是如何创建的。它有六到七种类型的复杂视图,这些视图可以多次出现(因此必须在运行时添加一些逻辑)。所以 总的来说,我必须在这个json的基础上做一个网格。

编辑 2 :- 这是 json 的一部分,在此基础上我必须制作一个 UI

 "decimal_variables": [
            {
                "id": 1, 
                "name": "Time Taken", 
                "var_type": 2, 
                "is_required": true, 
                "default": null, 
                "max_value": "10000.000000", 
                "min_value": "0.000000", 
                "unit": "sec", 
                "score_dependency": 2
            }, 
            {
                "id": 2, 
                "name": "Number of moves", 
                "var_type": 2, 
                "is_required": true, 
                "default": null, 
                "max_value": "1000.000000", 
                "min_value": "0.000000", 
                "unit": "moves", 
                "score_dependency": 2
            }, 
            {
                "id": 3, 
                "name": "Number of hints used", 
                "var_type": 2, 
                "is_required": true, 
                "default": null, 
                "max_value": "100.000000", 
                "min_value": "0.000000", 
                "unit": "hints", 
                "score_dependency": 2
            }
        ], 
        "choice_variables": [
            {
                "choices": [
                    {
                        "id": 1, 
                        "name": "Easy", 
                        "value": "1.000000"
                    }, 
                    {
                        "id": 2, 
                        "name": "Medium", 
                        "value": "2.000000"
                    }, 
                    {
                        "id": 3, 
                        "name": "Tough", 
                        "value": "3.000000"
                    }
                ], 
                "id": 1, 
                "name": "Difficulty level", 
                "var_type": 1, 
                "is_required": true, 
                "default": null, 
                "score_dependency": 3
            }
        ], 
        "boolean_variables": [
            {
                "id": 1, 
                "name": "Is Hint Allowed", 
                "var_type": 1, 
                "default": true, 
                "score_dependency": 3
            }
        ], 
        "text_variables": [
            {
                "id": 1, 
                "name": "Instructions to student", 
                "var_type": 1, 
                "is_required": false, 
                "default": "1. Carefully do the assignment.\r\n2. Time is key in assignment score, so make sure not to waste the time.", 
                "max_length": 1000, 
                "score_dependency": 3
            }
        ], 
        "file_variables": [
            {
                "default": "media/variable-files/supermegafunbits-1024x1024.png", 
                "id": 1, 
                "name": "Image", 
                "var_type": 1, 
                "is_required": false, 
                "file_type": "image/jpeg,image/png", 
                "score_dependency": 3
            }
        ], 

现在在文件变量中,我必须创建一个包含图像控件、浏览图像的按钮、标题文本块和信息文本块的 ui(它可能很复杂)。所以我正在寻找一个工具,如果我在其中从 xaml 制作 UI 可以大量时间转换为 C# 代码,并且可以节省不必要的编码。

【问题讨论】:

  • 无需在 WPF 的过程代码中创建或操作 UI 元素。这就是 XAML 的用途。发布您用于生成 UI 的相关数据和所需内容的屏幕截图,我可以告诉您在 WPF 中执行此操作的正确方法。

标签: c# wpf xaml


【解决方案1】:

为什么需要在代码中创建它?您可以创建一个数据模板。创建一个视图和一个视图模型,它们将给出视图的逻辑。在您的 ViewModel 中,您可以设置、获取、隐藏、修改 View。

我建议阅读这篇文章:

MVVM in Depth

WPF/MVVM Quick Start Tutorial

您好!

【讨论】:

  • 感谢@dachi,我明白你想说什么,但整个视图依赖于 json,所以不能在 xaml 中执行。
  • 让人们知道有一条“普遍接受”的路径很有用;但根据其本身的优点回答问题也很重要。
猜你喜欢
  • 2011-01-30
  • 2012-03-04
  • 1970-01-01
  • 2015-10-25
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
相关资源
最近更新 更多