简体   繁体   English

如何在Windows Phone中获取绑定到longlistselector的json链接数据?

[英]How to get json links data binded to longlistselector in windows phone?

I have three json links. 我有三个json链接。 In them, one link contains category(MenuCategory) namely (starters, maindishes) and two links contain (MenuItem)some data(Itemname, Price, Picture) in it. 在它们中,一个链接包含类别(MenuCategory),即(启动器,主菜),两个链接其中包含(MenuItem)一些数据(项目名称,价格,图片)。 You can see in the below image, starters and Maindishes are headers which come from one link and the items below them for starters and Maindishes comes from two different links(.../menuitems/1 and ../menuitems/2). 您可以在下图中看到,起动器和Maindishes是来自一个链接的标题,而起动器和Maindishes在它们下方的项目来自两个不同的链接(... / menuitems / 1和../menuitems/2)。 How to get this data into longlistselector as shown in the image. 如图所示,如何将这些数据放入longlistselector中。 I am getting the Json data into my app but i need to display them inside the LLS. 我正在将Json数据导入我的应用程序,但需要在LLS内显示它们。 How to combine them? 如何结合它们?

// http://xxxxxx.net/restaurant/category/1 // http://xxxxxx.net/restaurant/category/1

// http://xxxxxx.net//restaurant/menuitems/1 // and http://xxxxxx.net/restaurant/menuitems/2 // http://xxxxxx.net//restaurant/menuitems/1 //和http://xxxxxx.net/restaurant/menuitems/2

     public class MenuItem
        {
            public int Menuitemid { get; set; }
            public int Menucategoryid { get; set; }
            public string Itemname { get; set; }        
            public double Price { get; set; }
            public string Picture { get; set; }     
        }

        public class MenuCategory
        {
            public int Menucategoryid { get; set; }
            public int Menuid { get; set; }
            public string Categoryname { get; set; }
            public string Description { get; set; }
            public bool Active { get; set; }
            public string Createddate { get; set; }
            public object Modifieddate { get; set; }
        }



        public class MenuCategoryRootObject
        {
            public List<MenuCategory> data { get; set; }
        }

在此处输入图片说明

To use LongListSelector in grouping mode, you'll need to compose your data into a different form than it probably comes to your application. 要在分组模式下使用LongListSelector,您需要将数据组合成与应用程序可能不同的形式。 Ultimately, you will be binding your list control's ItemsSource to a property that can be seen as a List<List<T>> , where the outer List can be a standard List object, the inner List<T> objects should contain all the properties of your MenuCategory, and the type T should be of some class similar to MenuItem. 最终,您将列表控件的ItemsSource绑定到一个可以看作List<List<T>>的属性,其中外部List可以是标准List对象,内部List<T>对象应包含所有属性您的MenuCategory,并且类型T应该类似于MenuItem。 The following class implementations should give an example of one way to implement this goal. 下面的类实现应提供实现此目标的一种方法的示例。 The MenuItemModel should probably not inherit from MenuItem in your implementation, but this is just for the purposes of demonstration: MenuItemModel可能不应从实现中的MenuItem继承,但这只是出于演示的目的:

public class MenuCategoryModel : List<MenuItemModel>
{
    public MenuCategoryModel() { }

    public int Menucategoryid { get; set; }
    public int Menuid { get; set; }
    public string Categoryname { get; set; }
    public string Description { get; set; }
    public bool Active { get; set; }
    public string Createddate { get; set; }
    public object Modifieddate { get; set; }
}

public class MenuItemModel : MenuItem { }

If you are binding to a ViewModel, it would have a single property you could use as an ItemsSource for your LongListSelector, like so: 如果要绑定到ViewModel,它将具有单个属性,可以将其用作LongListSelector的ItemsSource,如下所示:

public class MyViewModel
{
    public MyViewModel() { }

    public List<MenuCategoryModel> Categories { get; set; }
}

From there, your XAML markup might look something like the following: 从那里开始,您的XAML标记可能类似于以下内容:

<phone:LongListSelector ItemsSource="{Binding Categories}"
                        IsGroupingEnabled="True">
    <phone:LongListSelector.GroupHeaderTemplate>
        <DataTemplate>
            <Grid Background="DarkSlateGray">
                <TextBlock Style="{StaticResource PhoneTextTitle3Style}"
                            Text="{Binding Categoryname}"
                            FontWeight="Bold" />
            </Grid>
        </DataTemplate>
    </phone:LongListSelector.GroupHeaderTemplate>
    <phone:LongListSelector.ItemTemplate>                   
        <DataTemplate>
            <Border Background="LightGray"
                    BorderBrush="DarkSlateGray"
                    BorderThickness="0 0 0 1">
                <StackPanel>
                    <TextBlock Style="{StaticResource PhoneTextTitle2Style}"
                                Foreground="DarkSlateGray"
                                Text="{Binding Itemname}" />
                    <TextBlock Style="{StaticResource PhoneTextTitle2Style}"
                                Foreground="DarkSlateGray"
                                Text="{Binding Price, StringFormat=C}" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

You can see the LongListSelector has IsGroupingEnabled set to true and definitions for GroupHeaderTemplate and ItemTemplate. 您可以看到LongListSelector的IsGroupingEnabled设置为true,以及GroupHeaderTemplate和ItemTemplate的定义。 You can even enable jump mode to allow users to tap the group headers and jump between them, but I have not demonstrated this capability here. 您甚至可以启用跳转模式,以允许用户点击组标题并在它们之间跳转,但是我在这里没有演示此功能。 Attached is a sample screenshot of what this implementation looks like in the designer. 附件是此实现在设计器中的外观的示例屏幕快照。

样本设计器输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定到LongListSelector / Listbox的项目数据未在Windows Phone 8本地数据库中显示 - Items Data Binded to LongListSelector/Listbox not showing in windows phone 8 local database 如何在Windows Phone中对LongListSelector进行排序 - How to Sort a LongListSelector in Windows Phone LongListSelector C#-如何从分组的数据列表中以字符串形式获取SelectedItem-Windows Phone Silverlight 8.1 - LongListSelector C# - How to get SelectedItem as string from grouped data list - Windows Phone Silverlight 8.1 在Windows Phone应用程序中的LongListselector中,如何为Textblock进行数据绑定? - How to do Data Binding for Textblock within a LongListselector in windows phone app? 将JSON字符串反序列化为LongListSelector Windows Phone 8 - Deserializing a JSON String to LongListSelector Windows Phone 8 LongListSelector不绑定数据数组Windows Phone 8 - LongListSelector not binding a array of data Windows Phone 8 如何将Json数据导入windows phone 8的listview? - How to get Json data into listview for windows phone 8? 如何在Windows Phone 8中获取Json数据 - How To Get Json Data in Windows Phone 8 如何在Windows Phone 7的LongListSelector中禁用滚动视图 - How to disable the scrollview in LongListSelector in windows phone 7 如何将项目动态添加到LongListSelector Windows Phone 8 - How to add items dynamically to LongListSelector Windows Phone 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM