none
LongListSelector acting very strange RRS feed

  • Pergunta

  • Hi,

    I’m experiencing a problem with the longlistselector witch is a bit strange... I’m reading a list asynchronously on a multi Pivot page, and if I don’t change the pivot until the resulto f the list Reading, it will create the contacts list successfuly ( in a longlistselector on the pivot item number 3 ) and when I go to that pivot the contacts list is displayed withou any problems, but when I open the Page and change the pivot before the longlistselector is created when the asychronous function returns the results and fills the longlistselector on pivot no.3 the contacts wont get updated ( when I go to pivot 3 no contacts are shown)...

    I’ll post my code so you can have a clearer picture of the situation and maybe figure out what is happening.

    The code is based in the PhoneToolkit LongListSelector example (buddies list)


    public partial class Feeds : PhoneApplicationPage
        {
            bool isLoading = false;
            bool loadingFilmates = true;
    
            public Feeds()
            {
                InitializeComponent();
                ApplicationBar = new ApplicationBar();
                StayfilmApplicationBar stayAppBar = new StayfilmApplicationBar(ApplicationBar, this);
                stayAppBar.BuildLocalizedApplicationBar();
    
    
                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);
                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;
            }

    Notice that I’ve even tried changing the Visibility property when it loads to force a re-render on the screen and it didn’t work.

    This is the StayObjectFilmates class:

    public class StayObjectFilmates
        {
            public string Img { get; private set; }
            public string Name { get; private set; }
            public Guid 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, Guid 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;
            }
    
        }


    This is the AllFilmates class:

    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, friend.idUser);
                            _filmateLookup[i] = f;
                            i++;
                        }
                    }
                }
            }

    And this is the AlphaKeyGroup.cs file :

    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;
            }
        }

    The longlistselector in the xaml page is inside a PivotItem and defined like this:

    <phone:LongListSelector x:Name="longlistFilmates"
                		HideEmptyGroups="True"
                		Foreground="{StaticResource StayfilmRedColor}"
                		ItemsSource="{Binding Filmates}"
                		IsGroupingEnabled="True"
                		toolkit:TiltEffect.IsTiltEnabled="True" Visibility="Collapsed" >
                        <phone:LongListSelector.GroupHeaderTemplate>
                            <DataTemplate>
                                <Grid Background="Transparent" Margin="12,8,0,8">
                                    <Border Background="{StaticResource StayfilmRedColor}" 	
                						Padding="8,0,0,0" Width="62" Height="62" 				 
                						HorizontalAlignment="Left">
                                        <TextBlock Text="{Binding Key}" 
                							Foreground="#FFFFFF" 
                							FontSize="38"
                							FontFamily="{StaticResource PhoneFontFamilySemiLight}"
                							HorizontalAlignment="Left"
                							VerticalAlignment="Bottom"/>
                                    </Border>
                                </Grid>
                            </DataTemplate>
                        </phone:LongListSelector.GroupHeaderTemplate>
                        <phone:LongListSelector.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="12,8,0,8" Background="Transparent">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Image Width="75" Height="75" Source="{Binding Img}" VerticalAlignment="Top"/>
                                    <StackPanel Grid.Column="1" VerticalAlignment="Top">
                                        <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Margin="12,-12,12,6" Foreground="{StaticResource StayfilmRedColor}"/>
                                    </StackPanel>
                                </Grid>
                            </DataTemplate>
                        </phone:LongListSelector.ItemTemplate>
                        <phone:LongListSelector.JumpListStyle>
                            <Style TargetType="phone:LongListSelector">
                                <Setter Property="ItemTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <Grid Background="{Binding Converter={StaticResource BackgroundConverter}}" Margin="6" toolkit:TiltEffect.IsTiltEnabled="True">
                                                <TextBlock Text="{Binding Key}" 
                									FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                									FontSize="48"
                									Margin="12,0,0,0"
                									Foreground="{Binding Converter={StaticResource ForegroundConverter}}" 
                									VerticalAlignment="Bottom"/>
                                            </Grid>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="LayoutMode" Value="Grid"/>
                                <Setter Property="GridCellSize" Value="111,111"/>
                                <Setter Property="Margin" Value="12,6,0,0"/>
                            </Style>
                        </phone:LongListSelector.JumpListStyle>
                    </phone:LongListSelector>

    The FilmatesInGroup.cs and FilmatesByName.cs  is the same as PeopleInGroup.cs and PeopleByFirstName.cs in the PhoneToolKit example with the names adapted.

    The longlistFilmates LongListSelector Object is inserted directly inside the PivotItem no.3 ( no Grid and no ScrollView )

    Thanks in advance for any help!


    • Editado Rickrvo sexta-feira, 4 de abril de 2014 21:32
    • Movido Giovani CrModerator quarta-feira, 9 de abril de 2014 19:33 off-topic
    sexta-feira, 4 de abril de 2014 21:04

Respostas

  • Hi Rickrvo,

    This is a Portuguese-Brazilian Forum. For questions in english, ask in MSDN en-US, you will be answered better:

    Msdn forums


    Herbert Lausmann

    sexta-feira, 4 de abril de 2014 22:22