【问题标题】:Dynamically add entry control in Xamarin在 Xamarin 中动态添加入口控件
【发布时间】:2021-08-01 12:09:22
【问题描述】:

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

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

更新

根据答案更新代码。

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;
    }
}

【问题讨论】:

  • 很高兴听到您想做什么,但您明确的问题是什么?到目前为止,您为实现目标做了哪些努力?

标签: c# json .net xamarin xamarin.forms


【解决方案1】:

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

将 json 转换为类,然后获取 json 数据列表。

在你的VS中,编辑>选择性粘贴>将JSON粘贴为类

 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的模板:

Xaml:

 <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 类。

【讨论】:

  • 感谢您的回复,但有一件事,您为什么要使用var json.. 我想从服务器获得这个“json”响应(它是动态的)
  • 我没有服务器来获取json 响应。所以我直接使用json字符串作为参考。
  • 大声笑.. 真的很抱歉.. 是的,现在,我使用你的代码并编译它,我收到了这个错误Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating Mobile.App.Views.Form.FormPage.'
  • 哪一行代码抛出了错误?下断点一步步调试。
  • 我在每一行都添加了断点,当打开页面并调用InitializeComponent();时,它会返回此错误..
猜你喜欢
  • 1970-01-01
  • 2011-05-17
  • 2013-05-29
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
相关资源
最近更新 更多