WP8 working with XML and LongListSelector

本文关键字:and LongListSelector XML with working WP8 | 更新日期: 2023-09-27 18:29:52

我正试图从xml文件数据中创建一个LongListSelector。

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但我在这条线上有个错误:

            countriesList.ItemsSource = data;

错误显示:

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

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

谢谢你的帮助:)

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

            <!--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>

WP8 working with XML and LongListSelector

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

countriesList.ItemsSource = data.ToList();

编辑:要回答您的评论,我不确定您的意思。

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

    <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>