-->

WP8 XML和LongListSelector工作(WP8 working with XML an

2019-11-02 11:02发布

我试图让从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>

Answer 1:

LongListSelector只能取一个列表作为数据源。 只需调用.ToList()在你的LINQ查询方法:

countriesList.ItemsSource = data.ToList();

编辑:要回答你的评论,我不知道你的意思。

你绑定LongListSelector到“项”,所以你需要暴露在你的视图模型的“项目”属性的国家名单。 然后,在LongListSelector的ItemTemplate中,绑定控件来贵国类的属性:

    <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 working with XML and LongListSelector