在 Xamarin 中动态添加入口控制 [英] Dynamically add entry control in Xamarin

查看:15
本文介绍了在 Xamarin 中动态添加入口控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 API 获取 XML Entry Control,在 JSON 中可用.

I want to fetch the XML Entry Control from API, available in JSON.

Entry 控制的所有属性都在 JSON 中.我想将它们添加到 .xml 页面中,并在 ViewModel(通过数据绑定)中获取它们的值(当用户进入应用程序时).

All the attributes for Entry control are in JSON. I want to add them in .xml page and get their values (when user enter in app) in ViewModel (through data binding).

更新

根据答案更新代码.

public partial class FormPage : ContentPage, IRootView
{
    public List<Form> forms { get; set; }

    public class RootObject
    {
        public bool success { get; set; }
        public Datum[] data { get; set; }
    }

    public class Datum
    {
        public Form[] form { get; set; }
    }

    public class Form
    {
        public string label { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public int max_length { get; set; }
        public bool required { get; set; }
    }

    public FormPage()
    {
        InitializeComponent();

        var json = @{};

        var list = JsonConvert.DeserializeObject<RootObject>(json);

        forms = new List<Form>();
        forms = list.data.FirstOrDefault().form.ToList();

        this.BindingContext = this;
    }
}

推荐答案

反序列化 Json: 从 NuGet 安装 Newtonsoft.Json.

Deserialize Json: Install Newtonsoft.Json from NuGet.

将json转成class,然后得到json数据列表.

Convert the json to classes and then get the list of the json data.

在你的 VS 中,编辑>粘贴特殊>将 JSON 粘贴为类

In your VS, Edit> Paste Special> Paste JSON As Classes

 public class Rootobject
{
    public bool success { get; set; }
    public Datum[] data { get; set; }
}

public class Datum
{
    public Form[] form { get; set; }
}

public class Form
{
    public string label { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public int max_length { get; set; }
    public bool required { get; set; }
}

反序列化以获取列表:

   var list = JsonConvert.DeserializeObject<Rootobject>(json);

使用StackLayout的Bindablelayout.ItemTemplate设置Entry的模板:

XML:

 <StackLayout x:Name="DynamicEntry" BindableLayout.ItemsSource="{Binding forms}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <Entry Placeholder="{Binding label}" MaxLength="{Binding max_length}" />

            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>

背后的代码:

 public List<Form> forms { get; set; }
    public Page1()
    {
        InitializeComponent();

        var json = @"{
'success': 'true',
'data': [
    {
        'form': [
            {
                'label': 'Name',
                'name': 'name',
                'type': 'text',
                'max_length': '15',
                'required': 'true'
            },
            {
                'label': 'Email',
                'name': 'email',
                'type': 'email',
                'max_length': '30',
                'required': 'true'
            }
        ]
    }
]
}";
        var list = JsonConvert.DeserializeObject<Rootobject>(json);

        forms = new List<Form>();
        forms = list.data.FirstOrDefault().form.ToList();

        this.BindingContext = this;
    }

请注意,如果您想设置不同类型的 Kayboard,您需要使用 Keyboard 类转换列表.

Please note if you want to set the Kayboard with different types, you need convert the list with Keyboard class.

这篇关于在 Xamarin 中动态添加入口控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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