WindowsPhone 8,LongListSelector和ObservableCollection很奇怪

时间:2013-06-04 22:14:45

标签: windows-phone-8 observablecollection longlistselector

任何人都可以解释为什么这种行为很奇怪吗?

它应该做的是增加一个计数并在行上输出,一次一个,每3个开始一个新行。即

1

1,2

1,2,3

1,2,3 4

1,2,3 4,5

1,2,3 4,5,6

正在做的是:

1

1

1

1 4

1,2,3 4,5

然后按预期进行,即

1,2,3 4,5,6

它在带有ListBox的Windows Phone 7中按预期工作,但它在Windows Phone 8中实现了这种奇怪。

我需要这个嵌套的集合构造,因此手动触发OnCollectionChanged。

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector HorizontalAlignment="Left" Height="524" Margin="10,10,0,0" VerticalAlignment="Top" Width="446" Name="listBox1">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="164,539,0,-4" Click="Button_Click"/>
    </Grid>
using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

namespace ObservableCollection
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Rounds rounds = new Rounds();
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            listBox1.ItemsSource = rounds;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            rounds.Add();
        }

        public class Round : Collection<int>
        {
            public string Name
            {
                get
                {
                    string s = "";
                    foreach (int i in this)
                    {
                        if (s.Length > 0)
                            s += ",";
                        s += i;
                    }
                    return s;
                }
            }
        }

        public class Rounds : ObservableCollection<Round>
        {
            public void Add()
            {
                int i = 1;

                if (Count > 0)
                    i = this.Last().Last() + 1;

                if (Count == 0 || this.Last().Count > 2)
                    Add(new Round());

                this.Last().Add(i);

                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
    }
}

0 个答案:

没有答案