-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainPage.xaml.cs
156 lines (135 loc) · 5.23 KB
/
MainPage.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
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;
namespace sbbs_client_wp7
{
using Sbbs;
using System.Collections.ObjectModel;
using System.Windows.Threading;
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Add Tilt effect for Tile
TiltEffect.TiltableItems.Add(typeof(Tile));
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
// 登录后刷新收藏夹
App.ViewModel.LoginChanged += delegate(object sender, bool isLogin)
{
LoadFavorates();
};
}
// Load data for the ViewModel Items
private bool isFirstLoading = true;
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// 启动时延迟2秒更新
if (isFirstLoading)
{
isFirstLoading = false;
// 延迟两秒后开始刷新全部
DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
timer.Tick += delegate(object s, EventArgs arg)
{
App.ViewModel.IsDataLoaded = false; // 标记开始更新
LoadTopten();
LoadFavorates();
timer.Stop();
};
timer.Start();
}
}
// 注销
private void Logout_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
MessageBoxResult result = MessageBox.Show("真的要注销吗?", "注销", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
App.ViewModel.IsLogin = false;
}
// 刷新十大
private void RefreshTopten_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
App.ViewModel.IsToptenLoaded = false;
LoadTopten();
}
// 刷新收藏夹
private void RefreshFavorates_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
App.ViewModel.IsFavoratesLoaded = false;
LoadFavorates();
}
private void Tile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
NavigationService.Navigate(new Uri("/" + (sender as Tile).Tag, UriKind.Relative));
}
// 点击收藏夹
private void Favorates_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
BoardViewModel board = e.AddedItems[0] as BoardViewModel;
this.NavigationService.Navigate(new Uri("/BoardPage.xaml?board=" + board.EnglishName + "&description=" + board.Description, UriKind.Relative));
}
}
// 点击十大
private void Topten_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 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));
// 清除选择,否则同样的项目无法点击第二次
(sender as ListBox).SelectedIndex = -1;
}
}
// 载入收藏夹
private void LoadFavorates()
{
// 登录时载入收藏夹,未登陆时清空
if (App.ViewModel.IsLogin)
{
App.Service.Favorates(delegate(ObservableCollection<BoardViewModel> boards, bool success, string error)
{
App.ViewModel.IsFavoratesLoaded = true;
if (error != null)
return;
LocalCache.Set<ObservableCollection<BoardViewModel>>("Favorates", boards);
App.ViewModel.FavoratesItems = boards;
});
}
else
{
App.ViewModel.IsFavoratesLoaded = true;
LocalCache.Set<ObservableCollection<BoardViewModel>>("Favorates", null);
App.ViewModel.FavoratesItems = null;
}
}
// 载入十大
public void LoadTopten()
{
App.Service.Topten(delegate(ObservableCollection<TopicViewModel> topics, bool success, string error)
{
App.ViewModel.IsToptenLoaded = true;
if (error != null)
return;
// 刷新十大
LocalCache.Set<ObservableCollection<TopicViewModel>>("Topten", topics);
App.ViewModel.ToptenItems = topics;
});
}
}
}