简体   繁体   中英

LongListSelector and ObservableCollection throws InvalidCastException

I'm trying to bind my collection to LLS, but I get an InvalidCastException. If i use ListBox instead of LLS - all binds work fine and good. Here is my collection:

    public class Friend : INotifyPropertyChanged
{
    private string name;
    public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } }
    private string image;
    public string Image { get { return image; } set { image = value; OnPropertyChanged("Image"); } }
    private string sourseId;
    public string SourseId { get { return sourseId; } set { sourseId = value; OnPropertyChanged("SourseId"); } }
    private string online_image;
    public string Online_Image { get { return online_image; } set { online_image = value; if (online_image == "1") online_image = @"/icons/appbar.power.png"; OnPropertyChanged("Online_Image"); } }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
    public Friend() { }


}
public class FriendList : ObservableCollection<Friend>
{
    public FriendList()
        : base()
    {

    }
}

And here is the way i bind it to LLS:

            XDocument xml = XDocument.Load(e.Result);
      //  MessageBox.Show(xml.ToString());
        var users = from c in xml.Descendants("user")
                    select c;
        foreach (var user in users)
        {
            Friend fr = new Friend();
            fr.SourseId = (string)user.Element("uid").Value;
            fr.Image = (string)user.Element("photo_medium").Value;
            fr.Name = (string)user.Element("first_name").Value + " " + (string)user.Element("last_name").Value;
            fr.Online_Image = (string)user.Element("online").Value;
            if (fr.Online_Image == "1") fr.Online_Image = @"/icons/appbar.power.png";
            FriendList.Add(fr);
        }
        this.AllFriendsList.ItemsSource = FriendList;

and public FriendList FriendList = new FriendList(); is just under the page ctor. What i'm doing wrong? Exception fires on string this.AllFriendsList.ItemsSource = FriendList;

And here is my xaml:

                    <toolkit:LongListSelector Name="AllFriendsList" ItemsSource="{Binding FriendList}">
                    <toolkit:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <Grid Height="75" Width="460" Margin="0,10,10,0">
                                <Line Style="{StaticResource Line1}" ></Line>
                                <Line Style="{StaticResource Line2}" ></Line>
                                <TextBlock Margin="75,15,40,0" FontSize="30" Name="Name" Text="{Binding Name}" Tap="GetUserInfo" />
                                <Image HorizontalAlignment="Left" Width="75" Name="Photo" >
                                    <Image.Source>
                                        <BitmapImage UriSource="{Binding Image}" CreateOptions="BackgroundCreation" />
                                    </Image.Source>
                                </Image>
                                <Image HorizontalAlignment="Right" Name="IsOnline" Margin="0,0,0,0" Width="60" Height="60" Source="{Binding Online_Image}" />
                            </Grid>
                        </DataTemplate>
                    </toolkit:LongListSelector.ItemTemplate>
                </toolkit:LongListSelector>

UPD: If I use group such as in this example, and group my list like

            var FriendsGroup = from fr in FriendList
                            group fr by fr.Online_Image into c
                            orderby c.Key
                            select new Group<Friend>(c.Key, c);

        AllFriendsList.ItemsSource = FriendsGroup;

it works fine. If I understand correct - LLS waiting a grouped source.

我通过将IsFlat更改为True解决了相同的问题。

<toolkit:LongListSelector Name="AllFriendsList" ItemsSource="{Binding FriendList}" IsFlat="True">

You are binding twice to the your AllFiendsList. The first time through the xaml here

<toolkit:LongListSelector Name="AllFriendsList" ItemsSource="{Binding FriendList}">

The second time through code. That may cause the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM