将Json数据绑定到windows phone 8.1中的TextBlock

本文关键字:中的 TextBlock phone windows Json 数据绑定 | 更新日期: 2023-09-27 18:21:57

我通过REST web服务获取数据,需要在列表视图中显示数据。我的问题是我无法绑定数据。我得到的错误是-错误:

 Error: BindingExpression path error: 'id' property not found on 'Windows.Foundation.IReference`1<String>'. BindingExpression: Path='id' DataItem='Windows.Foundation.IReference`1<String>'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')

型号代码:

    public class Deal
    {
        public String id { get; set; }            
        public string businessName { get; set; }
    }

XAML代码:

        <ListView Name="ItemData"
            HorizontalAlignment="Left" 
            Height="640" 
            VerticalAlignment="Top" 
            Width="396">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Width="100" FontSize="22" Text="Deal ID:"/>
                        <TextBlock Width="100" FontSize="22" Text="{Binding id}"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>    
        </ListView>

CS功能:

        private async void MakeListData()
        {
            HttpConnection httpConnect = new HttpConnection();
            String result = await httpConnect.ReadDataFromWeb();  
            DealDataModel rootObject = JsonConvert.DeserializeObject<DealDataModel>(result);  
            List<Deal> dealList = rootObject.deals;
            List<string> idList = new List<string>();
            for (int i = 0; i < dealList.LongCount(); i++)
            {
                idList.Add(dealList[i].id);
            }
            ItemData.ItemsSource = idList;
        }

如果有人能帮我克服这个问题,我将不胜感激。感谢

将Json数据绑定到windows phone 8.1中的TextBlock

您的代码将List<string>用于ItemsSource,而string没有属性id,这就是绑定引擎找不到id属性的原因。我想你想把ItemsSource设置为dealList,哪种类型是List<Data>:

ItemData.ItemsSource = dealList;