WP8使用XML和LongListSelector

时间:2013-09-02 17:51:22

标签: c# xml windows-phone-8 longlistselector

我正在尝试从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>

1 个答案:

答案 0 :(得分:1)

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

countriesList.ItemsSource = data.ToList();

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

您将LongListSelector绑定到“Items”,因此您需要在viewmodel的“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>