Skip to content

Commit

Permalink
显示话题
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Mar 3, 2012
1 parent 2dc615b commit b18b166
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 32 deletions.
48 changes: 47 additions & 1 deletion App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,37 @@
<local:LeafBrushConverter x:Key="LeafBrushConverter" />
<local:BoolVisibleConverter x:Key="BoolVisibleConverter" />
<local:BoolReverseConverter x:Key="BoolReverseConverter" />

<local:StampDateConverter x:Key="StampDateConverter" />

<!--RichTextBlock默认样式-->
<Style TargetType="RichTextBox">
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" />
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RichTextBox">
<Grid Background="Transparent">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Margin="{StaticResource PhoneHorizontalMargin}">
<ContentControl x:Name="ContentElement"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Padding="{TemplateBinding Padding}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<!--话题列表Template-->
<DataTemplate x:Key="TopicDataTemplate">
<Grid Margin="0 0 0 10">
Expand All @@ -32,6 +62,22 @@
</Grid>
</DataTemplate>

<!--话题内容Template-->
<DataTemplate x:Key="ContentDataTemplate">
<StackPanel>
<Grid Margin="-12 0 0 0">
<TextBlock Style="{StaticResource PhoneTextAccentStyle}" Text="{Binding Author}"/>
<TextBlock HorizontalAlignment="Right" Text="{Binding Time, Converter={StaticResource StampDateConverter}}"/>
</Grid>
<RichTextBox Margin="-12 0 0 0">
<Paragraph>
<Run Text="{Binding Content}"/>
</Paragraph>
</RichTextBox>
<Rectangle Margin="0 2 0 8" HorizontalAlignment="Stretch" Height="1" Fill="{StaticResource PhoneForegroundBrush}" />
</StackPanel>
</DataTemplate>

<!--版面列表Template-->
<DataTemplate x:Key="BoardDataTemplate">
<Grid Margin="0 0 0 10">
Expand Down
3 changes: 2 additions & 1 deletion BoardPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
<ScrollViewer Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<ListBox ItemsSource="{Binding Topics}" ItemTemplate="{StaticResource TopicDataTemplate}"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
ScrollViewer.VerticalScrollBarVisibility="Disabled"
SelectionChanged="Topic_Selected">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
Expand Down
36 changes: 26 additions & 10 deletions BoardPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
if (this.NavigationContext.QueryString.ContainsKey("board"))
{
string board = this.NavigationContext.QueryString["board"];
LoadMore.IsEnabled = false;

// 跳转到其他版面时清空并重载
if (board != App.ViewModel.CurrentBoard.EnglishName) {
// 重置标题
App.ViewModel.CurrentBoard.EnglishName = board;
App.ViewModel.CurrentBoard.Description = this.NavigationContext.QueryString["description"];

// 还原加载按钮
LoadMore.IsEnabled = true;
LoadMore.IsEnabled = false;
App.ViewModel.CurrentBoard.IsLoaded = false;

// 清空已有内容
if (App.ViewModel.CurrentBoard.Topics != null)
Expand All @@ -57,6 +55,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
// 重新加载
App.Service.Board(board, currentPage * pageSize, pageSize, delegate(ObservableCollection<TopicViewModel> topics, bool success, string error)
{
// 还原加载按钮
LoadMore.IsEnabled = true;

App.ViewModel.CurrentBoard.IsLoaded = true;
if (error == null)
App.ViewModel.CurrentBoard.Topics = topics;
Expand All @@ -68,27 +69,42 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
private void LoadMore_Click(object sender, RoutedEventArgs 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.IsEnabled = false;
else
LoadMore.IsEnabled = true;

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

// 判断后面时候还有内容
if (topics.Count < pageSize)
LoadMore.IsEnabled = false;

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

private void Topic_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
// 清除选择,否则同样的项目无法点击第二次
(sender as ListBox).SelectedIndex = -1;
TopicViewModel topic = e.AddedItems[0] as TopicViewModel;

this.NavigationService.Navigate(
new Uri("/TopicPage.xaml?board=" + topic.Board + "&id=" + topic.Id + "&title=" + HttpUtility.UrlEncode(topic.Title), UriKind.Relative));
}
}

}
}
3 changes: 2 additions & 1 deletion MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
<controls:PanoramaItem Header="十大热帖" HeaderTemplate="{StaticResource MyItemHeaderTemplate}">
<ScrollViewer>
<ListBox ItemsSource="{Binding ToptenItems}" ItemTemplate="{StaticResource TopicDataTemplate}"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
ScrollViewer.VerticalScrollBarVisibility="Disabled"
SelectionChanged="Topten_Selected">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
Expand Down
15 changes: 15 additions & 0 deletions MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ private void Favorates_Selected(object sender, SelectionChangedEventArgs e)
}
}

// 点击十大
private void Topten_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
// 清除选择,否则同样的项目无法点击第二次
(sender as ListBox).SelectedIndex = -1;

TopicViewModel topic = e.AddedItems[0] as TopicViewModel;

this.NavigationService.Navigate(
new Uri("/TopicPage.xaml?board=" + topic.Board + "&id=" + topic.Id + "&title=" + HttpUtility.UrlEncode(topic.Title), UriKind.Relative));
}
}

// 载入收藏夹
private void LoadFavorates()
{
Expand Down
22 changes: 11 additions & 11 deletions SampleData/CurrentTopicViewModelSampleData.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
Title="8G的U盘现在的市场价是多少?">

<local:CurrentTopicViewModel.Topics>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="20" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="13" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="14" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Title="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="20" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="13" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="14" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
<sbbs:TopicViewModel Time="1330767901" Content="江南听雨BBS技术站务2011年下半年总结" Author="moqi88" Board="BBSView" Id="43647" Replies="10" Read="100" Top="False" Mark="False"/>
</local:CurrentTopicViewModel.Topics>

</local:CurrentTopicViewModel>
10 changes: 10 additions & 0 deletions Sbbs/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public void Board(string board, int start, int limit, Action<TopicCollection, bo
wc.DownloadStringAsync(uri, new ServiceArg<TopicCollection>() { Callback = callback });
}

// 获取话题
public void Topic(string board, int id, int start, int limit, Action<TopicCollection, bool, string> callback)
{
WebClient wc = new WebClient();
Uri uri = new Uri(apiBase + "topic/" + board + "/" + id + apiPost + "?token=" + HttpUtility.UrlEncode(Token) + "&start=" + start + "&limit=" + limit);

wc.DownloadStringCompleted += DownloadedAndParse<TopicCollection, TopicsResponse>;
wc.DownloadStringAsync(uri, new ServiceArg<TopicCollection>() { Callback = callback });
}

// 下载完成后分析JSON数据然后调用回调函数
// C为返回类型,比如TopicCollection
// R为JSON的Response类型
Expand Down
18 changes: 14 additions & 4 deletions TopicPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"
toolkit:TiltEffect.IsTiltEnabled="True"
d:DataContext="{d:DesignData SampleData/CurrentTopicViewModelSampleData.xaml}">

<shell:SystemTray.ProgressIndicator>
Expand Down Expand Up @@ -55,9 +54,20 @@
<TextBlock x:Name="BoardName" Text="{Binding Board}" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="BoardDescription" Text="{Binding Title}" TextWrapping="Wrap" Margin="9,-7,0,0" Foreground="{StaticResource PhoneAccentBrush}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>

<ScrollViewer Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<ListBox ItemsSource="{Binding Topics}" ItemTemplate="{StaticResource ContentDataTemplate}"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button toolkit:TiltEffect.IsTiltEnabled="True" Margin="-12 0" x:Name="LoadMore" Content="载入更多" Click="LoadMore_Click"/>
</StackPanel>
</ScrollViewer>
</Grid>

<phone:PhoneApplicationPage.ApplicationBar>
Expand Down
80 changes: 76 additions & 4 deletions TopicPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,93 @@
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;

namespace sbbs_client_wp7
{
using Sbbs;

public partial class TopicPage : PhoneApplicationPage
{
// 每页显示多少话题
const int pageSize = 10;
// 当前页数
int currentPage = 0;

public TopicPage()
{
InitializeComponent();

DataContext = App.ViewModel.CurrentTopic;
}

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

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

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

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);

if (this.NavigationContext.QueryString.ContainsKey("board"))
{
string board = this.NavigationContext.QueryString["board"];
int id = int.Parse(this.NavigationContext.QueryString["id"]);

// 跳转到其他话题时清空并重载
if (board != App.ViewModel.CurrentTopic.Board || id != App.ViewModel.CurrentTopic.Id)
{
// 重置标题
App.ViewModel.CurrentTopic.Id = id;
App.ViewModel.CurrentTopic.Board = board;
App.ViewModel.CurrentTopic.Title = this.NavigationContext.QueryString["title"];

App.ViewModel.CurrentTopic.IsLoaded = false;
LoadMore.IsEnabled = false;

// 清空已有内容
if (App.ViewModel.CurrentTopic.Topics != null)
App.ViewModel.CurrentTopic.Topics.Clear();

// 重新加载
App.Service.Topic(board, id, currentPage * pageSize, pageSize, delegate(ObservableCollection<TopicViewModel> topics, bool success, string error)
{
// 还原加载按钮
LoadMore.IsEnabled = true;

App.ViewModel.CurrentTopic.IsLoaded = true;
if (error == null)
App.ViewModel.CurrentTopic.Topics = topics;
});
}
}
}
}
}
Loading

0 comments on commit b18b166

Please sign in to comment.