WP8 使用 XML 和 LongListSelector [英] WP8 working with XML and LongListSelector

查看:18
本文介绍了WP8 使用 XML 和 LongListSelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 xml 文件数据创建一个 LongListSelector.

I'm trying to make a LongListSelector from xml file data.

public MainPage()
    {
        InitializeComponent();

        XDocument loadedData = XDocument.Load("/Resources/EuroTrip.xml");

        var data = from query in loadedData.Descendants("country")
                   select new Country
                   {
                       Name = (string)query.Element("name"),
                       IdentityCard = (string)query.Element("identityCard"),
                       CompulsoryDocuments = (string)query.Element("compulsoryDocuments"),
                       Regulations = (string)query.Element("regulations"),
                       Equipment = (string)query.Element("equipment"),
                       SpeedLimitsLightVehicles = (string)query.Element("speedLimitsLightVehicles"),
                       AutoClubs = (string)query.Element("autoClubs")
                   };
        countriesList.ItemsSource = data;
        // Set the data context of the listbox control to the sample data
        DataContext = App.ViewModel;
    }

    public class Country
{
    string name;
    string identityCard;
    string compulsoryDocuments;
    string regulations;
    string equipment;
    string speedLimitsLightVehicles;
    string autoClubs;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string IdentityCard
    {
        get { return identityCard; }
        set { identityCard = value; }
    }

    public string CompulsoryDocuments
    {
        get { return compulsoryDocuments; }
        set { compulsoryDocuments = value; }
    }

    public string Regulations
    {
        get { return regulations; }
        set { regulations = value; }
    }

    public string Equipment
    {
        get { return equipment; }
        set { equipment = value; }
    }

    public string SpeedLimitsLightVehicles
    {
        get { return speedLimitsLightVehicles; }
        set { speedLimitsLightVehicles = value; }
    }

    public string AutoClubs
    {
        get { return autoClubs; }
        set { autoClubs = value; }
    }
    }

我使用了本教程:http://www.geekchamp.com/tips/wp7-working-with-xml-reading-filtering-and-databinding但我在这一行遇到错误:

I used this tutorial: http://www.geekchamp.com/tips/wp7-working-with-xml-reading-filtering-and-databinding but I got an error on this line:

            countriesList.ItemsSource = data;

错误说:

存在显式转换(您是否缺少强制转换?)

An explicit conversion exists (are you missing a cast?)

我认为是因为 WP7 和 WP8 中的 LongListSelector 没有使用相同的控件,但我不知道我必须更改什么,也找不到任何关于此的有用文章.

I think because the LongListSelector in WP7 and WP8 aren't using the same controls, but I don't know what I must change and I don't find any useful article about this.

感谢您的帮助:)

这是我想要绑定数据的 xaml 代码:

this is the xaml code where I would want to bind the data:

            <!--Pivot item two-->
        <phone:PivotItem Header="Countries">
            <!--Double line list no text wrapping-->
            <phone:LongListSelector IsGroupingEnabled="False" x:Name="countriesList" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17">
                            <!--Image Width="60" Source="{Binding Photo}" Margin="12,6" HorizontalAlignment="Left"/-->
                            <TextBlock VerticalAlignment="Center" Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="82,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="Black"/>
                            <TextBlock VerticalAlignment="Center" Text="{Binding LineTwo}" TextWrapping="NoWrap" Margin="82,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="Black"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PivotItem>

推荐答案

LongListSelector 只能以列表作为数据源.只需在您的 Linq 查询中调用 .ToList() 方法:

The LongListSelector can only take a list as data source. Just call the .ToList() method on your Linq query:

countriesList.ItemsSource = data.ToList();

为了回答您的评论,我不确定您的意思.

To answer your comment, I'm not sure what you mean.

您将 LongListSelector 绑定到Items",因此您需要在视图模型的Items"属性中公开您的国家/地区列表.然后,在 LongListSelector 的 ItemTemplate 中,将控件绑定到 Country 类的属性:

You're binding the LongListSelector to "Items", so you need to expose your list of countries in the "Items" property of your viewmodel. Then, in the ItemTemplate of the LongListSelector, bind the controls to the properties of your Country class:

    <phone:PivotItem Header="Countries">
        <phone:LongListSelector IsGroupingEnabled="False" x:Name="countriesList" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                        <!-- Displays the name of the country -->
                        <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </phone:PivotItem>

这篇关于WP8 使用 XML 和 LongListSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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