visual studio 2012 -如何使用c#在windows phone应用程序中添加列表项

本文关键字:应用程序 phone 添加 列表 windows 2012 studio 何使用 visual | 更新日期: 2023-09-27 17:51:10

我是c#和windows phone编程的初学者,我有一个代码,我将JSON数据加载到消息框中,我能够做到这一点,但是我想在消息框中显示这些数据到页面中的列表项,我无法理解msdn中的API,帮助我从这个

程序

XAML CODE

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,12,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
           </Grid>
</Grid>
<<p> c代码/strong>
 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.Runtime.Serialization.Json;
    using System.IO;
    using System.Text;
    using System.Runtime.Serialization;
    namespace JsonArray
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                string jsonString = @"{
        'product': [
            {
                'CATEGORYNAME': [
                    'AshokLeyland',
                    'HindustanMotorsLtd',
                    'MahindraLtd',
                    'TataMotors',
                    'SwarajMazda'
                ],
                'CATEGORYID': [
                    '1',
                    '2',
                    '3',
                    '4',
                    '6'
                ],
                'SUBCATEGORYNAME': [
                    'MultiaxleVehicles',
                    'HippoHaulage',
                    'HippoTipper',
                    'Cargo',
                    'Pick-up'
                ],
                'SUBCATEGORYID': [
                    '1',
                    '2',
                    '3',
                    '4',
                    '5'
                ],
                'TYPENAME': [
                    'Haulage',
                    'Tippers',
                    'RigidTrucks',
                    'Cabs',
                    'DeliveryVan'
                ],
                'TYPEID': [
                    '1',
                    '2',
                    '3',
                    '4',
                    '5'
                ]
            }
        ],
        'success': 1,
        'message': 'Thetruckdetailsgettingsuccessfully'
    }
    "; ;
                RootObject TotalList = new RootObject();
                RootObject childlistonly = new RootObject();
                Product prdt = new Product();
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(TotalList.GetType());
                TotalList = ser.ReadObject(ms) as RootObject;
                string category = "";
                string typename = "";
                int i = 0;
                int k = 0;
                foreach (var d in TotalList.product)
                {
                    foreach (var t in d.CATEGORYNAME)
                    {
                        category = category + "" + t.ToString() + "'n" + "'n" + "'n";
                        i++;
                    }
                    foreach (var t in d.TYPENAME)
                    {
                        typename = typename + "" + t.ToString() + "'n" + "'n" + "'n";
                        k++;
                    }
                    foreach (var t in d.SUBCATEGORYNAME)
                    {
                        // teststring = teststring + "" + t.ToString() + "'n" + "'n" + "'n";
                        i++;
                    }
                }
                MessageBox.Show("Your CATEGORYNAMES are:'n'n" + category);
                MessageBox.Show("Your TYPENAMES are:'n'n" + typename);
                ms.Close();

            }
        }
        public class Product
        {
            public List<string> CATEGORYNAME { get; set; }
            public List<string> CATEGORYID { get; set; }
            public List<string> SUBCATEGORYNAME { get; set; }
            public List<string> SUBCATEGORYID { get; set; }
            public List<string> TYPENAME { get; set; }
            public List<string> TYPEID { get; set; }
        }
        public class RootObject
        {
            public List<Product> product { get; set; }
            public int success { get; set; }
            public string message { get; set; }
        }
    }

visual studio 2012 -如何使用c#在windows phone应用程序中添加列表项

您需要使用LongListSelector或ListBox

这里有一些教程

http://msdn.microsoft.com/en-us/library/windowsphone/design/hh202882%28v=vs.105%29.aspx

http://msdn.microsoft.com/en-us/library/windows/apps/hh868196.aspx

http://developer.nokia.com/community/wiki/Listbox_handling_in_Windows_Phone

http://msdn.microsoft.com/en-us/library/windowsphone/design/jj735577%28v=vs.105%29.aspx

http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj244365%28v=vs.105%29.aspx

http://blogs.windows.com/windows_phone/b/wpdev/archive/2013/05/23/windows-phone-8-xaml-longlistselector.aspx

另外,您可以查看这个系列-

http://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners

它给出了Windows Phone的一个很好的概述

编辑:

在xaml页面中,您需要添加一个LongListSelector。

一个简单的例子-

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector Name="YourList" LayoutMode="List" IsGroupingEnabled="False">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Grid>
              //Add Textblocks here
            </Grid>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>

-

YourList。