1

我正在使用 Windows Phone Toolkit nuget 包(http://phone.codeplex.com/)。我想在我的 WP8 应用程序中创建一个 LoopingSelector,我可以在其中选择日期,但我不希望 LoopingSelector 在它的选择中显示今天之前的几天。

我的控件如下所示:

http://s2.postimg.org/6bgnxefnt/image.png

这是完美的工作,直到我选择了源的第一个元素(今天 12 月 4 日),然后我的应用程序变得无响应(我可以按下按钮,显示效果,但事件处理程序不会执行)。我猜这是一个无限循环或死锁,但它不在我自己的代码中(我调试过它)。

LoopingSelector的数据源定义:

public class NextDatesDataSource : LoopingDataSource<NextDateModel>
{
    public NextDatesDataSource() : base(NextDateModel.Create(DateTime.Today)) { }
    protected override NextDateModel GetNext(NextDateModel relativeTo)
    {
        if (relativeTo.Date == DateTime.Today + TimeSpan.FromDays(10))
            return null;
        return NextDateModel.Create(relativeTo.Date + TimeSpan.FromDays(1));
    }
    protected override NextDateModel GetPrevious(NextDateModel relativeTo)
    {
        //if i comment the next two lines, then everything works perfect
        if (relativeTo.Date == DateTime.Today)
            return null;
        return NextDateModel.Create(relativeTo.Date - TimeSpan.FromDays(1));
    }
}

循环数据源:

public abstract class LoopingDataSource<T> : ILoopingSelectorDataSource
{
    protected LoopingDataSource() { selectedItem = default(T); }
    protected LoopingDataSource(T initialSelection) { selectedItem = initialSelection; }

    protected abstract T GetNext(T relativeTo);
    protected abstract T GetPrevious(T relativeTo);

    public object GetNext(object relativeTo)
    {
        if (relativeTo == null) return null;
        return GetNext((T)relativeTo);
    }

    public object GetPrevious(object relativeTo)
    {
        if (relativeTo == null) return null;
        return GetPrevious((T)relativeTo);
    }

    private T selectedItem;
    public object SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            object oldVal = selectedItem;
            selectedItem = (T)value;
            if (SelectionChanged != null)
                SelectionChanged(this, new SelectionChangedEventArgs(new object[] { oldVal }, new object[] { value }));
        }
    }
    public T Selected
    {
        get { return (T)SelectedItem; }
        set { SelectedItem = value; }
    }

    public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
}

控制元素的定义:

   <p:LoopingSelector
            x:Name="DaySelector"
            Grid.Column="0" Grid.Row="1"
            ItemSize="230,110"
            ItemMargin="6" Margin="10,5,5,5">
        <p:LoopingSelector.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding Header}" FontSize="28" VerticalAlignment="Top"/>
                    <TextBlock Text="{Binding Body}" FontSize="42" FontWeight="Bold" VerticalAlignment="Bottom"/>
                </Grid>
            </DataTemplate>
        </p:LoopingSelector.ItemTemplate>
    </p:LoopingSelector>

我在构造函数中这样做:

        DaySelector.DataSource = new NextDatesDataSource();

非常感谢!

4

0 回答 0