LongListSelector表现奇怪

时间:2014-04-03 21:14:07

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

我遇到了longlistselector问题,这有点奇怪......我正在多个Pivot页面上异步读取列表,如果我不更改数据库直到结果列表读取,它将成功创建联系人列表(在枢轴项目编号3的长列表选择器中),当我转到该枢轴时,联系人列表显示没有任何问题,但是当我打开页面并在创建长列表选择器之前更改枢轴时asychronous函数返回结果并填充第3号轴上的longlistselector,联系人不会更新(当我转到第3段时,没有显示任何联系人)......

我会发布我的代码,以便您可以更清楚地了解情况,并可能弄清楚发生了什么。 该代码基于PhoneToolkit LongListSelector示例(好友列表)

public partial class Feeds : PhoneApplicationPage
{
    bool isLoading = false;
    bool loadingFilmates = true;

    public Feeds()
    {
        InitializeComponent();

        // ...

        Loaded += FeedsPage_Loaded;
        SystemTray.ProgressIndicator = new ProgressIndicator();

        DataContext = this;

        getFilmatesList();

        longlistFilmates.SelectionChanged += FilmateSelectionChanged;

        // ...
   }

   private async void getFilmatesList()
   {
        Feed userFilmates = await Feed.GetFilmates(App.Current.AppUser, App.Current.WSConfig, 0, "", 10000); // read feeds from webservice
        Filmates = AlphaKeyGroup<StayObjectFilmates>.CreateGroups(AllFilmates.GetCurrent(userFilmates), CultureInfo.CurrentUICulture, (p) => { return p.Name; }, true);
        //longlistFilmates.Visibility = System.Windows.Visibility.Collapsed;
        longlistFilmates.Visibility = System.Windows.Visibility.Visible;
        longlistFilmates.UseLayoutRounding = true;
        pivotFeed.Visibility = System.Windows.Visibility.Collapsed;
        pivotFeed.Visibility = System.Windows.Visibility.Visible;
    }
}

请注意,我甚至尝试在加载时更改Visibility属性以强制在屏幕上重新渲染,但它不起作用。

这是StayObjectFilmates类:

public class StayObjectFilmates
{
    public string Img { get; private set; }
    public string Name { get; private set; }
    public string UserId { get; private set; }
    public string Id { get; set; }
    public User user { get; set; }

    public StayObjectFilmates()
    {
        //Img = "";
        //Name = "";
        //Id = "";
    }

    public StayObjectFilmates(string p_img, string p_name, string p_Id)
    {
        Img = p_img;
        Name = p_name;
        UserId = p_Id;
    }
    public StayObjectFilmates(User p_user)
    {
        user = p_user;
    }

    public static string GetNameKey(StayObjectFilmates filmate)
    {
        char key = char.ToLower(filmate.Name[0]);

        if (key < 'a' || key > 'z')
        {
            key = '#';
        }

        return key.ToString();
    }

    public static int CompareByName(object obj1, object obj2)
    {
        StayObjectFilmates p1 = (StayObjectFilmates)obj1;
        StayObjectFilmates p2 = (StayObjectFilmates)obj2;

        int result = p1.Name.CompareTo(p2.Name);
        if (result == 0)
        {
            result = p1.Img.CompareTo(p2.Img);
        }

        return result;
    }    
}

这是AllFilmates课程:

public class AllFilmates : IEnumerable<StayObjectFilmates>
{
    private static Dictionary<int, StayObjectFilmates> _filmateLookup;
    private static AllFilmates _instance;
    private Feed filmates;

    //  public List<StayObjectFilmates> Filmates { get; private set; }

    public static AllFilmates GetCurrent(Feed p_filmates)
    {
        if (_instance == null)
        {
            _instance = new AllFilmates();

        }

        _instance.filmates = p_filmates;

        return _instance;
    }

    public static AllFilmates Current
    {
        get
        {
            return _instance ?? (_instance = new AllFilmates());
        }
    }

    public StayObjectFilmates this[int index]
    {
        get
        {
            StayObjectFilmates filmate;
            _filmateLookup.TryGetValue(index, out filmate);
            return filmate;
        }
    }

    #region IEnumerable<StayObjectFilmates> Members

    public IEnumerator<StayObjectFilmates> GetEnumerator()
    {
        EnsureData();
        return _filmateLookup.Values.GetEnumerator();
    }
    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        EnsureData();
        return _filmateLookup.Values.GetEnumerator();
    }

    #endregion

    private void EnsureData()
    {
        if (_filmateLookup == null)
        {
            _filmateLookup = new Dictionary<int, StayObjectFilmates>();

            if (filmates != null)
            {
                int i = 0;
                foreach (var item in filmates.itemsList)
                {
                    User friend = item as User;
                    string userphoto = (friend.photo == null) ? "Images/avatar.jpg" : friend.photo;
                    StayObjectFilmates f = new StayObjectFilmates(userphoto, friend.fullName, i.ToString());
                    _filmateLookup[i] = f;
                    i++;
                }
            }

        }
    }
}

这是AlphaKeyGroup.cs文件:

public class AlphaKeyGroup<T> : List<T>
{
    private const string GlobeGroupKey = "\uD83C\uDF10";

    /// <summary>
    /// The delegate that is used to get the key information.
    /// </summary>
    /// <param name="item">An object of type T</param>
    /// <returns>The key value to use for this object</returns>
    public delegate string GetKeyDelegate(T item);

    /// <summary>
    /// The Key of this group.
    /// </summary>
    public string Key { get; private set; }

    /// <summary>
    /// Public constructor.
    /// </summary>
    /// <param name="key">The key for this group.</param>
    public AlphaKeyGroup(string key)
    {
        Key = key;
    }
    public AlphaKeyGroup(IGrouping<string, T> grouping)
    {
        Key = grouping.Key;
        this.AddRange(grouping);
    }

    /// <summary>
    /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
    /// </summary>
    /// <param name="slg">The </param>
    /// <returns>Theitems source for a LongListSelector</returns>
    private static List<AlphaKeyGroup<T>> CreateGroups(SortedLocaleGrouping slg)
    {
        List<AlphaKeyGroup<T>> list = new List<AlphaKeyGroup<T>>();

        foreach (string key in slg.GroupDisplayNames)
        {
            if (key == "...")
            {
                list.Add(new AlphaKeyGroup<T>(GlobeGroupKey));
            }
            else
            {
                list.Add(new AlphaKeyGroup<T>(key));
            }
        }

        return list;
    }

    /// <summary>
    /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
    /// </summary>
    /// <param name="items">The items to place in the groups.</param>
    /// <param name="ci">The CultureInfo to group and sort by.</param>
    /// <param name="getKey">A delegate to get the key from an item.</param>
    /// <param name="sort">Will sort the data if true.</param>
    /// <returns>An items source for a LongListSelector</returns>
    public static List<AlphaKeyGroup<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
    {
        SortedLocaleGrouping slg = new SortedLocaleGrouping(ci);
        List<AlphaKeyGroup<T>> list = CreateGroups(slg);

        foreach (T item in items)
        {
            int index = 0;
            if (slg.SupportsPhonetics)
            {
                //check if your database has yomi string for item
                //if it does not, then do you want to generate Yomi or ask the user for this item.
                //index = slg.GetGroupIndex(getKey(Yomiof(item)));
            }
            else
            {
                index = slg.GetGroupIndex(getKey(item));
            }
            if (index >= 0 && index < list.Count)
            {
                list[index].Add(item);
            }
        }

        if (sort)
        {
            foreach (AlphaKeyGroup<T> group in list)
            {
                group.Sort((c0, c1) => { return ci.CompareInfo.Compare(getKey(c0), getKey(c1)); });
            }
        }

        return list;
    }
}

FilmatesInGroup.cs和FilmatesByName.cs与PhoneToolKit示例中的PeopleInGroup.cs和PeopleByFirstName.cs相同,并且名称已经过修改。 longlistFilmates LongListSelector对象直接插入PivotItem no.3(没有Grid,没有ScrollView)

提前感谢您的帮助!

0 个答案:

没有答案