LoopingSelector在同一个地方的所有项目

时间:2013-12-29 14:11:44

标签: c# .net xaml windows-phone-8

我在Windows Phone 8中有一个使用LoopingSelector的应用程序。现在我想选择应该通过代码选择的项目,因此我设置了数据源的选定项目。

然而,当我这样做时,所有物品都堆叠在一起。有没有其他人之前遇到过这个问题,你是如何解决的?

编辑,我确实错过了DataSource代码 我的JokeLoopingSource(继承自我的BaseDataSource

public class JokeLoopingSource : LoopingDataSourceBase
{
    private readonly IList<JokeEntity> _objects;

    public JokeLoopingSource(IList<JokeEntity> objects)
    {
        _objects = objects;
        SelectedItem = _objects.FirstOrDefault();
    }

    public void SetSelectedItem(int index)
    {
        SelectedItem = _objects[index];
    }

    public override object GetNext(object relativeTo)
    {
        JokeEntity item = relativeTo as JokeEntity;
        if (item == null || item.Index >= _objects.Count-1)
        {
            return _objects[0];
        }

        return _objects[item.Index + 1];
    }

    public override object GetPrevious(object relativeTo)
    {
        JokeEntity item = relativeTo as JokeEntity;

        if (item == null || item.Index == 0)
        {
            return _objects.LastOrDefault();
        }

        return _objects[item.Index - 1];
    }
}

我的BaseDataSource

public abstract class LoopingDataSourceBase : ILoopingSelectorDataSource
{
    private object _selectedItem;

    public abstract object GetNext(object relativeTo);
    public abstract object GetPrevious(object relativeTo);
    public object SelectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
            if (!Equals(_selectedItem, value))
            {
                object previousSelectedItem = _selectedItem;
                _selectedItem = value;

                OnSelectionChanged(previousSelectedItem, _selectedItem);
            }
        }
    }

    public event EventHandler<SelectionChangedEventArgs> SelectionChanged;

    protected virtual void OnSelectionChanged(object oldSelectedItem, object newSelectedItem)
    {
        EventHandler<SelectionChangedEventArgs> handler = SelectionChanged;
        if (handler != null)
        {
            handler(this, new SelectionChangedEventArgs(new[] { oldSelectedItem }, new[] { newSelectedItem }));
        }
    }
}

0 个答案:

没有答案