forked from zcbenz/sbbs-client-wp7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardPage.xaml.cs
140 lines (119 loc) · 4.64 KB
/
BoardPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Controls;
using System.Windows.Documents;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
namespace sbbs_client_wp7
{
using Sbbs;
using CustomControls;
public partial class BoardPage : PhoneApplicationPage
{
// 每页显示多少话题
const int pageSize = 10;
// 当前页数
int currentPage = 0;
public BoardPage()
{
InitializeComponent();
DataContext = App.ViewModel.CurrentBoard;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// 从发帖页面返回,且需要刷新
if (App.ViewModel.CurrentBoard.NeedRefresh)
{
App.ViewModel.CurrentBoard.NeedRefresh = false;
Refresh_Click(null, null);
}
// 浏览版面
else if (this.NavigationContext.QueryString.ContainsKey("board"))
{
string board = this.NavigationContext.QueryString["board"];
// 跳转到其他版面时清空并重载
if (board != App.ViewModel.CurrentBoard.EnglishName) {
// 重置标题
App.ViewModel.CurrentBoard.EnglishName = board;
App.ViewModel.CurrentBoard.Description = this.NavigationContext.QueryString["description"];
// 清空已有内容
if (App.ViewModel.CurrentBoard.Topics != null)
App.ViewModel.CurrentBoard.Topics.Clear();
currentPage = 0;
LoadTopics();
}
}
}
private void Refresh_Click(object sender, EventArgs e)
{
currentPage = 0;
LoadTopics();
}
private void NewPost_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/PostPage.xaml?board=" + App.ViewModel.CurrentBoard.EnglishName, UriKind.Relative));
}
private void Settings_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/BoardSettingsPage.xaml", UriKind.Relative));
}
private void Clear_Click(object sender, EventArgs e)
{
App.Service.BoardMarkRead(App.ViewModel.CurrentBoard.EnglishName);
foreach (TopicViewModel topic in App.ViewModel.CurrentBoard.Topics)
{
topic.Unread = false;
}
}
private void TopicsList_NextPage(object sendor, NextPageEventArgs e)
{
LoadTopics(true);
}
private void TopicsList_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
// 清除选择,否则同样的项目无法点击第二次
(sender as ListBox).SelectedIndex = -1;
TopicViewModel topic = e.AddedItems[0] as TopicViewModel;
// 清除未读
topic.Unread = false;
this.NavigationService.Navigate(
new Uri("/TopicPage.xaml?board=" + topic.Board + "&id=" + topic.Id + "&title=" + HttpUtility.UrlEncode(topic.Title), UriKind.Relative));
}
}
private void LoadTopics(bool append = false)
{
if (App.ViewModel.CurrentBoard.IsLoading)
return;
App.ViewModel.CurrentBoard.IsLoading = true;
// 重新加载
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;
// 判断后面是否还有内容
TopicsList.IsFullyLoaded = error == null && topics.Count < pageSize;
if (error == null)
// 重置还是添加
if (append)
{
++currentPage;
foreach (TopicViewModel topic in topics)
App.ViewModel.CurrentBoard.Topics.Add(topic);
}
else
{
App.ViewModel.CurrentBoard.Topics = topics;
}
else
MessageBox.Show("网络错误");
});
}
}
}