Skip to content

Commit

Permalink
版面列表自动翻页
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Mar 8, 2012
1 parent a1f01a5 commit 82dbd1e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 90 deletions.
12 changes: 6 additions & 6 deletions BoardPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
d:DataContext="{d:DesignData SampleData/CurrentBoardViewModelSampleData.xaml}"
shell:SystemTray.IsVisible="True"
shell:SystemTray.Opacity="{Binding IsLoaded, Converter={StaticResource LoadedOpacityConerter}}"
shell:SystemTray.Opacity="{Binding IsLoading, Converter={StaticResource LoadedOpacityConerter}, ConverterParameter=true}"
toolkit:TiltEffect.IsTiltEnabled="True">

<shell:SystemTray.ProgressIndicator>
<shell:ProgressIndicator Text="载入中..."
IsVisible="{Binding IsLoaded, Converter={StaticResource BoolReverseConverter}}"
IsIndeterminate="{Binding IsLoaded, Converter={StaticResource BoolReverseConverter}}"/>
IsVisible="{Binding IsLoading}"
IsIndeterminate="{Binding IsLoading}"/>
</shell:SystemTray.ProgressIndicator>

<toolkit:TransitionService.NavigationInTransition>
Expand Down Expand Up @@ -60,16 +60,16 @@
</StackPanel>

<!--ContentPanel - place additional content here-->
<local:ExtendedListBox ItemsSource="{Binding Topics}" ItemTemplate="{StaticResource TopicDataTemplate}"
SelectionChanged="Topic_Selected"
<local:ExtendedListBox x:Name="TopicsList" ItemsSource="{Binding Topics}" ItemTemplate="{StaticResource TopicDataTemplate}"
SelectionChanged="TopicsList_Selected"
NextPage="TopicsList_NextPage"
Grid.Row="1" Margin="12,0,12,0">
<local:ExtendedListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</local:ExtendedListBox.ItemContainerStyle>
</local:ExtendedListBox>
<Button Grid.Row="2" Visibility="Collapsed" Margin="-12 0" x:Name="LoadMore" Content="载入更多" Click="LoadMore_Click"/>
</Grid>

<phone:PhoneApplicationPage.ApplicationBar>
Expand Down
76 changes: 29 additions & 47 deletions BoardPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,40 +82,12 @@ private void Clear_Click(object sender, EventArgs e)
}
}

private void LoadMore_Click(object sender, RoutedEventArgs e)
private void TopicsList_NextPage(object sendor, NextPageEventArgs e)
{
App.ViewModel.CurrentBoard.IsLoaded = false;
LoadMore.IsEnabled = false;
App.Service.Board(App.ViewModel.CurrentBoard.EnglishName, (currentPage + 1)* pageSize, pageSize, delegate(ObservableCollection<TopicViewModel> topics, bool success, string error)
{
// 判断后面是否还有内容
if (error == null && topics.Count < pageSize)
{
LoadMore.Visibility = Visibility.Collapsed;
LoadMore.IsEnabled = false;
}
else
{
LoadMore.Visibility = Visibility.Visible;
LoadMore.IsEnabled = true;
}

App.ViewModel.CurrentBoard.IsLoaded = true;
if (error == null)
{
currentPage++;

foreach (TopicViewModel topic in topics)
App.ViewModel.CurrentBoard.Topics.Add(topic);
}
else
{
MessageBox.Show("网络错误");
}
});
LoadTopics(true);
}

private void Topic_Selected(object sender, SelectionChangedEventArgs e)
private void TopicsList_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
Expand All @@ -131,29 +103,39 @@ private void Topic_Selected(object sender, SelectionChangedEventArgs e)
}
}

private void LoadTopics()
private void LoadTopics(bool append = false)
{
LoadMore.IsEnabled = false;
App.ViewModel.CurrentBoard.IsLoaded = false;
if (App.ViewModel.CurrentBoard.IsLoading)
return;

App.ViewModel.CurrentBoard.IsLoading = true;

// 重新加载
App.Service.Board(App.ViewModel.CurrentBoard.EnglishName, currentPage * pageSize, pageSize, delegate(ObservableCollection<TopicViewModel> topics, bool success, string error)
int page = append ? currentPage + 1 : currentPage;
App.Service.Board(App.ViewModel.CurrentBoard.EnglishName, page * pageSize, pageSize, delegate(ObservableCollection<TopicViewModel> topics, bool success, string error)
{
App.ViewModel.CurrentBoard.IsLoading = false;

// 判断后面是否还有内容
if (error == null && topics.Count < pageSize)
{
LoadMore.Visibility = Visibility.Collapsed;
LoadMore.IsEnabled = false;
}
else
{
LoadMore.Visibility = Visibility.Visible;
LoadMore.IsEnabled = true;
}
TopicsList.IsFullyLoaded = error == null && topics.Count < pageSize;

App.ViewModel.CurrentBoard.IsLoaded = true;
if (error == null)
App.ViewModel.CurrentBoard.Topics = topics;
// 重置还是添加
if (append)
{
++currentPage;
foreach (TopicViewModel topic in topics)
App.ViewModel.CurrentBoard.Topics.Add(topic);

// 叠加完毕时往后翻页

}
else
{
App.ViewModel.CurrentBoard.Topics = topics;
}
else
MessageBox.Show("网络错误");
});
}
}
Expand Down
61 changes: 30 additions & 31 deletions Controls/ExtendedListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,34 @@ public class ExtendedListBox : ListBox
protected bool _isBouncy = false;
private bool alreadyHookedScrollEvents = false;

TextBlock LoadMoreText;

// 属性:是否完全载入
public bool IsFullyLoaded
{
get { return (bool)GetValue(IsFullyLoadedProperty); }
set { SetValue(IsFullyLoadedProperty, value); }
}

public static readonly DependencyProperty IsFullyLoadedProperty =
DependencyProperty.Register("IsFullyLoaded", typeof(bool), typeof(ExtendedListBox), new PropertyMetadata(false));

// 事件:拖到边界处
public delegate void OnNextPage(object sender, NextPageEventArgs e);
public event OnNextPage NextPage;

public ExtendedListBox()
{
this.Loaded += new RoutedEventHandler(ListBox_Loaded);
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

LoadMoreText = (TextBlock)GetTemplateChild("LoadMoreText");
}

private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
ScrollBar sb = null;
Expand Down Expand Up @@ -54,45 +77,29 @@ private void ListBox_Loaded(object sender, RoutedEventArgs e)

}

public delegate void OnCompression(object sender, CompressionEventArgs e);
public event OnCompression Compression;

private void hgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e)
{
if (e.NewState.Name == "CompressionLeft")
{
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Left));
}

if (e.NewState.Name == "CompressionRight")
{
else if (e.NewState.Name == "CompressionRight")
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Right));
}
if (e.NewState.Name == "NoHorizontalCompression")
{
else if (e.NewState.Name == "NoHorizontalCompression")
_isBouncy = false;
}
}

private void vgroup_CurrentStateChanging(object sender, VisualStateChangedEventArgs e)
{
if (e.NewState.Name == "CompressionTop")
{
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Top));
}
if (e.NewState.Name == "CompressionBottom")
else if (e.NewState.Name == "CompressionBottom")
{
_isBouncy = true;
if (Compression != null)
Compression(this, new CompressionEventArgs(CompressionType.Bottom));
if (NextPage != null && !IsFullyLoaded)
NextPage(this, new NextPageEventArgs());
}
if (e.NewState.Name == "NoVerticalCompression")
else if (e.NewState.Name == "NoVerticalCompression")
_isBouncy = false;
}

Expand Down Expand Up @@ -138,15 +145,7 @@ private VisualStateGroup FindVisualState(FrameworkElement element, string name)
}
}

public class CompressionEventArgs : EventArgs
public class NextPageEventArgs : EventArgs
{
public CompressionType Type { get; protected set; }

public CompressionEventArgs(CompressionType type)
{
Type = type;
}
}

public enum CompressionType { Top, Bottom, Left, Right };
}
1 change: 1 addition & 0 deletions Controls/UIPropertyMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 6 additions & 6 deletions ViewModels/CurrentBoardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CurrentBoardViewModel : INotifyPropertyChanged
{
private string name;
private string description;
private bool isLoaded;
private bool isLoading;
private ObservableCollection<TopicViewModel> topics;

// 全局变量,用于标记跳转到版面时是否需要刷新
Expand Down Expand Up @@ -70,18 +70,18 @@ public ObservableCollection<TopicViewModel> Topics
}
}

public bool IsLoaded
public bool IsLoading
{
get
{
return isLoaded;
return isLoading;
}
set
{
if (isLoaded != value)
if (isLoading != value)
{
isLoaded = value;
NotifyPropertyChanged("IsLoaded");
isLoading = value;
NotifyPropertyChanged("IsLoading");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions sbbs-client-wp7.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<DependentUpon>BoardSettingsPage.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\ExtendedListBox.cs" />
<Compile Include="Controls\UIPropertyMetadata.cs" />
<Compile Include="DynamicOrientationChanges\AnimateOrientationChangesFrame.cs" />
<Compile Include="DynamicOrientationChanges\FadeOrientationChangesFrame.cs" />
<Compile Include="DynamicOrientationChanges\HybridOrientationChangesFrame.cs" />
Expand Down

0 comments on commit 82dbd1e

Please sign in to comment.