0

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

1 回答 1

1

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

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>
于 2013-09-02T18:51:04.120 回答