【问题标题】:windows phone 8 data binding to an arraywindows phone 8 数据绑定到数组
【发布时间】:2013-09-23 19:50:09
【问题描述】:

我是 windows phone 8 新手,目前正在为数组的 xaml 数据绑定而苦苦挣扎。

XAML:

<phone:LongListSelector x:Name="LocationsData" Margin="0,0,-12,0" ItemsSource="{Binding}">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
             <StackPanel Margin="0,0,0,17">
                 <TextBlock Text="{Binding name}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                 <TextBlock Text="{Binding vicinity}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
             </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

.CS

LocationsData.DataContext = _array_of_locations;

_array_of_locations 有 4 个项目,

每个都是有效的 JSON,

{
    "geometry": {
        "location": {
            "lat": 12.923101,
            "lng": 77.586057
        }
    },
    "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
    "id": "9a769bc2faaa79fe0ffbd85c2c28446940153331",
    "name": "Domino's Pizza",
    "opening_hours": {
        "open_now": false
    },
    "price_level": 1,
    "rating": 2.9,
    "reference": "CnRsAAAAsI0LvfwZ_RC8PEDsJS3TfKkRkTn7d_2_-vw8tu_SYBYCJk2CmKt6RyRJtO5mG0Weq-R0jSsmyQOWHjU45itlrH1cN89EqgIA9Vtmvcih1xi6ZwpNewqZ8mNCQWWLDJvcT3AQLHGnFcn4E9a30Gvs9xIQeKGVsrSOKLDx4vYCjixIKhoUOGeosCJIVFDmE3-3qIPcIM7PSCs",
    "types": [
        "restaurant",
        "food",
        "establishment"
    ],
    "vicinity": "11th Main Road, Jayanagar, Bangalore"
}

它不起作用,因为我不是来自 c# 背景,所以我迷失了所有文档。那么我如何让这个工作呢?

【问题讨论】:

    标签: xaml windows-phone-8 windows-phone datacontext


    【解决方案1】:

    问题不在于数组,而在于数据绑定的来源(即{Binding name}{Binding vicinity} 中的namevicinity)必须是property。数组中的对象没有名称为 namevicinity 的属性,因此不会显示任何内容。事实上,您甚至可能会在 Visual Studio 的“输出”窗口中看到绑定错误,指出它找不到 namevicinity 属性。

    我怀疑您的LongListSelector 中有四个项目,但是因为其中没有任何文本,所以它们的大小为零并且您看不到它们。尝试在您的StackPanel 中添加带有一些静态文本(例如&lt;TextBlock Text="X"/&gt;)的TextBlock,看看是否出现了四个X

    如果您只需要以只读方式显示 JSON,那么应该可以将 JSON 反序列化为 anonymous types。然后将为每个 JSON 对象创建一个匿名类型,并为 JSON 对象中的每个属性创建一个 (.NET) 属性。参见例如this question。但是,如果运气好的话,我还没有对其进行测试。

    【讨论】:

    • 很酷,会给你这个,并尽量让你知道。感谢您抽出宝贵时间,非常感谢 :)
    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 2013-12-30
    • 1970-01-01
    相关资源
    最近更新 更多