我正在使用Windows Phone Toolkit nuget包(http://phone.codeplex.com/)。我想在我的WP8应用程序中创建一个LoopingSelector,在那里我可以选择日期,但我不希望LoopingSelector在它的选择之前显示几天。
我的控件如下:
http://s2.postimg.org/6bgnxefnt/image.png
这是完全有效的,直到我选择源的第一个元素(今天12月04日),然后我的应用程序变得没有响应(我可以按下按钮,显示效果,但事件处理程序将不会执行)。我想这是一个无限循环或死锁,但它不在我自己的代码中(我调试它)。
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));
}
}
LoopingDataSource:
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();
非常感谢!