根据来自Web服务Xamarin Forms的数据动态显示标签 [英] Dynamic Presentation of Label as per the Data coming from web services Xamarin Forms

查看:77
本文介绍了根据来自Web服务Xamarin Forms的数据动态显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在框架内呈现信息列表,数据库中的信息分为两部分,即描述":值" 因此列表中的行使我成为一组描述和值,有些可能没有

i am trying to render list of information inside a frame the information which comes up from database is in two parts ie "Description" : "Value" so the row inside list make i group of description and value and some may not have

所以有人可以帮助我如何根据我从服务中获得的数据将标签添加到网格或堆栈中,因为有时可能是3,有时可能是5,创建标签并将其绑定将是静态的职位

so can anyone help me how can i add the label to grid or stack as per the data which i get from my services because it could be 3 sometimes and it could be 5 sometime and creating label and binding them would be a static job

推荐答案

如果您可以使用xamarin Forms3.5或更高版本的BindableLayout,则有一个非常明确的答案.

There is a very clean answer if you can use xamarin forms3.5 or greater, BindableLayout.

Xaml文件

<ListView
            x:Name="listView"
            HasUnevenRows="True"
            ItemsSource="{Binding Category}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding Name}" TextColor="Blue" />
                            <StackLayout BindableLayout.ItemsSource="{Binding users}">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <Label Text="{Binding Name}" />
                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

在我的ViewModel中

In my ViewModel

public class DynamicSwitchViewModel:BaseViewModel
    {
        public DynamicSwitchViewModel(ContentPage view):base(view)
        {
            ObservableCollection<User> users1 = new ObservableCollection<User>();
            users1.Add(new User() { Name = "karan3" });
            users1.Add(new User() { Name = "karan4" });
            users1.Add(new User() { Name = "karan5" });
            ObservableCollection<User> users2 = new ObservableCollection<User>();
            users2.Add(new User() { Name = "karan1" });
            users2.Add(new User() { Name = "karan2" });
            users2.Add(new User() { Name = "karan3" });
            users2.Add(new User() { Name = "karan4" });
            users2.Add(new User() { Name = "karan5" });
            ObservableCollection<User> users3 = new ObservableCollection<User>();
            users3.Add(new User() { Name = "karan1" });
            users3.Add(new User() { Name = "karan2" });
            users3.Add(new User() { Name = "karan3" });
            Category = new ObservableCollection<Category>();
            Category.Add(new Category() { Name = "1",users=users1 });
            Category.Add(new Category() { Name = "2",users=users2 });
            Category.Add(new Category() { Name = "3",users=users3 });


        }
        private ObservableCollection<Category> category;
        public ObservableCollection<Category> Category
        {
            get { return category; }
            set { SetProperty(ref category, value); }
        }

    }
    public class Category
    {
        public  ObservableCollection<User> users { get; set; }
        public string Name { get; set; }
    }
    public class User
    {
        public string Name { get; set; }
    }

这篇关于根据来自Web服务Xamarin Forms的数据动态显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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