从 ViewModel Xamarin 中的 Dynaimc 表单获取条目值 [英] Get the Entry Value from Dynaimc Form in ViewModel Xamarin

查看:31
本文介绍了从 ViewModel Xamarin 中的 Dynaimc 表单获取条目值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Xamarin 应用程序中,我从服务器获取 动态表单 &添加 XML 条目控件.

In my Xamarin App, I'm getting Dynamic Form from Server & add in XML Entry Control.

有多个 Entry 并且它们是动态的(可以是任意数字),因为我不知道数字和它们的名称,所以我无法在 ViewModel 中编写可绑定属性来获取进入的价值.

There are multiple Entry and they are dynamic (it could be of any number), as I don't know the number and their names, I can't write bindable properties in ViewModel to get the Value of Entry.

有没有办法从动态表单中获取Entry的值?

Is there any way to get the value of Entry from dynamic form?

视图模型

编码从 API 获取动态字段

Code the get the dynamic fields from API

Uri uri = new Uri(string.Format("http://example.com/entry"));

HttpClient httpClient = new HttpClient(new NativeMessageHandler());

HttpResponseMessage response = await httpClient.GetAsync(uri);
string entries = await response.Content.ReadAsStringAsync();

RootObject list = JsonConvert.DeserializeObject<RootObject>(entries);

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

public List<Form> forms { get; set; }

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

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

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

推荐答案

在@Jason的意见和建议的帮助下,终于解决了.

With the help of @Jason's comments and suggestions, finally solved it.

使用 ObservableCollection 获取 &设置动态表单.

Using ObservableCollection to get & set the Dynamic Form.

private ObservableCollection<Form> _form = new ObservableCollection<Form>();
public ObservableCollection<Form> form
{
    get => _form;
    set => this.RaiseAndSetIfChanged(ref _form, value);
}

名为Value"的表单类的属性.将 Entry 的 Text 属性绑定到 Value.

Property on Form class named "Value". Binded the Text property of the Entry to Value.

<Entry Text="{Binding Value}" />

public class Form
{
    // Code
    public string Value { get; set; }
}

public ObservableCollection<Form> Value
{
    get => _form;
    set => this.RaiseAndSetIfChanged(ref _form, value);
}

通过引用该列表获取值的代码

Code to get the values by referring to that list

foreach (var item in Value.ToList())
{
    item.Value.ToString();
}

这篇关于从 ViewModel Xamarin 中的 Dynaimc 表单获取条目值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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