From ca96302628d688b16ac2d5a0ec37a6556518821b Mon Sep 17 00:00:00 2001 From: Zhao Cheng Date: Sat, 3 Mar 2012 22:32:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=97=AA=E7=83=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml | 5 +- BoardPage.xaml | 4 +- BoardPage.xaml.cs | 12 +- LocalCache.cs | 30 +++++ LoginPage.xaml.cs | 16 ++- MainPage.xaml | 154 ++++++++++---------------- MainPage.xaml.cs | 16 +-- Sbbs/Service.cs | 2 +- TopicPage.xaml | 4 +- TopicPage.xaml.cs | 7 +- ValueConverters/StampDateConverter.cs | 6 +- ViewModels/MainViewModel.cs | 41 ++----- sbbs-client-wp7.csproj | 1 + 13 files changed, 138 insertions(+), 160 deletions(-) create mode 100644 LocalCache.cs diff --git a/App.xaml b/App.xaml index c2a6b1c..1872b54 100644 --- a/App.xaml +++ b/App.xaml @@ -49,7 +49,7 @@ - + @@ -69,12 +69,11 @@ - + - diff --git a/BoardPage.xaml b/BoardPage.xaml index 4972878..93396c2 100644 --- a/BoardPage.xaml +++ b/BoardPage.xaml @@ -52,8 +52,8 @@ - - + + diff --git a/BoardPage.xaml.cs b/BoardPage.xaml.cs index f5c76c8..c2a3cde 100644 --- a/BoardPage.xaml.cs +++ b/BoardPage.xaml.cs @@ -25,11 +25,6 @@ public BoardPage() InitializeComponent(); DataContext = App.ViewModel.CurrentBoard; - this.Loaded += new RoutedEventHandler(Page_Loaded); - } - - private void Page_Loaded(object sender, RoutedEventArgs e) - { } protected override void OnNavigatedTo(NavigationEventArgs e) @@ -55,8 +50,11 @@ protected override void OnNavigatedTo(NavigationEventArgs e) // 重新加载 App.Service.Board(board, currentPage * pageSize, pageSize, delegate(ObservableCollection topics, bool success, string error) { - // 还原加载按钮 - LoadMore.IsEnabled = true; + // 判断后面是否还有内容 + if (error == null && topics.Count < pageSize) + LoadMore.IsEnabled = false; + else + LoadMore.IsEnabled = true; App.ViewModel.CurrentBoard.IsLoaded = true; if (error == null) diff --git a/LocalCache.cs b/LocalCache.cs new file mode 100644 index 0000000..2c7314d --- /dev/null +++ b/LocalCache.cs @@ -0,0 +1,30 @@ +using System.IO.IsolatedStorage; + +namespace sbbs_client_wp7 +{ + public static class LocalCache + { + public static T Get(string key, T defaultValue) + { + T value; + if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(key, out value)) + IsolatedStorageSettings.ApplicationSettings[key] = value = defaultValue; + + return value; + } + + public static T Get(string key) + { + T value; + if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(key, out value)) + value = default(T); + + return value; + } + + public static void Set(string key, T value) + { + IsolatedStorageSettings.ApplicationSettings[key] = value; + } + } +} diff --git a/LoginPage.xaml.cs b/LoginPage.xaml.cs index c23cd65..3f06453 100644 --- a/LoginPage.xaml.cs +++ b/LoginPage.xaml.cs @@ -24,19 +24,27 @@ public LoginPage() private void Login_Click(object sender, RoutedEventArgs e) { - string username = Username.Text; - string password = Password.Password; - App.ViewModel.Login(username, password, delegate(string error) + App.ViewModel.IsLogining = true; + App.Service.Login(Username.Text, Password.Password, delegate(string token, bool success, string error) { + App.ViewModel.IsLogining = false; if (error == null) { + // 保存获得的Token + App.Service.Token = token; + LocalCache.Set("Token", token); + App.ViewModel.IsLogin = true; this.NavigationService.GoBack(); } - else + else if (!success) { MessageBox.Show("用户名密码错误"); } + else + { + MessageBox.Show("网络错误"); + } }); } } diff --git a/MainPage.xaml b/MainPage.xaml index 3443482..8620bd6 100644 --- a/MainPage.xaml +++ b/MainPage.xaml @@ -48,105 +48,65 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + - - - - - - + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/MainPage.xaml.cs b/MainPage.xaml.cs index 4e58ca5..445db20 100644 --- a/MainPage.xaml.cs +++ b/MainPage.xaml.cs @@ -56,7 +56,9 @@ private void Login_Click(object sender, MouseButtonEventArgs e) // 注销 private void Logout_Click(object sender, MouseButtonEventArgs e) { - App.ViewModel.IsLogin = false; + MessageBoxResult result = MessageBox.Show("真的要注销吗?", "注销", MessageBoxButton.OKCancel); + if (result == MessageBoxResult.OK) + App.ViewModel.IsLogin = false; } // 刷新十大 @@ -78,15 +80,15 @@ private void Favorates_Selected(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count == 1) { - // 清除选择,否则同样的项目无法点击第二次 - (sender as ListBox).SelectedIndex = -1; - BoardViewModel board = e.AddedItems[0] as BoardViewModel; // 收藏夹目录暂时不管 if (board.Leaf != true) return; this.NavigationService.Navigate(new Uri("/BoardPage.xaml?board=" + board.EnglishName + "&description=" + board.Description, UriKind.Relative)); + + // 清除选择,否则同样的项目无法点击第二次 + (sender as ListBox).SelectedIndex = -1; } } @@ -95,13 +97,13 @@ 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)); + + // 清除选择,否则同样的项目无法点击第二次 + (sender as ListBox).SelectedIndex = -1; } } diff --git a/Sbbs/Service.cs b/Sbbs/Service.cs index a2018eb..fd8ecae 100644 --- a/Sbbs/Service.cs +++ b/Sbbs/Service.cs @@ -102,7 +102,7 @@ void DownloadedAndParse(object sender, DownloadStringCompletedEventArgs e) if (result.error != null) { // result.error表示有错误 - arg.Callback(default(C), false, result.error); + arg.Callback(default(C), true, result.error); } else { diff --git a/TopicPage.xaml b/TopicPage.xaml index 7909e58..cf6c751 100644 --- a/TopicPage.xaml +++ b/TopicPage.xaml @@ -51,8 +51,8 @@ - - + + diff --git a/TopicPage.xaml.cs b/TopicPage.xaml.cs index 653392c..46c5321 100644 --- a/TopicPage.xaml.cs +++ b/TopicPage.xaml.cs @@ -81,8 +81,11 @@ protected override void OnNavigatedTo(NavigationEventArgs e) // 重新加载 App.Service.Topic(board, id, currentPage * pageSize, pageSize, delegate(ObservableCollection topics, bool success, string error) { - // 还原加载按钮 - LoadMore.IsEnabled = true; + // 判断后面是否还有内容 + if (error == null && topics.Count < pageSize) + LoadMore.IsEnabled = false; + else + LoadMore.IsEnabled = true; App.ViewModel.CurrentTopic.IsLoaded = true; if (error == null) diff --git a/ValueConverters/StampDateConverter.cs b/ValueConverters/StampDateConverter.cs index d3839d0..093b9da 100644 --- a/ValueConverters/StampDateConverter.cs +++ b/ValueConverters/StampDateConverter.cs @@ -22,11 +22,11 @@ public object Convert(object value, Type targetType, object parameter, CultureIn DateTime now = new DateTime(); if (date.Year == now.Year && date.Month == now.Month && date.Day == now.Day) - return date.ToString("t", culture); + return date.ToString("HH:mm", culture); else if (date.Year == now.Year) - return date.ToString("m t", culture); + return date.ToString("m HH:mm", culture); else - return date.ToString("y t", culture); + return date.ToString("y HH:mm", culture); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index f06227e..f24a513 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -21,9 +21,8 @@ public class MainViewModel : INotifyPropertyChanged { public MainViewModel() { - // 临时设置登录 - this.isLogin = true; - App.Service.Token = "Zm9vbA==:==wdlloHYLkEW0n2ltyx5QKO"; + // 载入保存的设置 + App.Service.Token = LocalCache.Get("Token"); // 初始化 CurrentBoard = new CurrentBoardViewModel(); @@ -103,30 +102,22 @@ public ObservableCollection FavoratesItems public CurrentTopicViewModel CurrentTopic { get; set; } // 是否已经登陆 - private bool isLogin; public bool IsLogin { get { - return isLogin; + return App.Service.Token != null && App.Service.Token != ""; } set { - if (isLogin != value) - { - isLogin = value; - - // 启动所有登录钩子 - LoginChanged(this, isLogin); + // 启动所有登录钩子 + LoginChanged(this, value); - // 注销时清除Token - if (isLogin == false) - { - App.Service.Token = null; - } + // 注销时清除Token + if (value == false) + App.Service.Token = null; - NotifyPropertyChanged("IsLogin"); - } + NotifyPropertyChanged("IsLogin"); } } // 是否正在登录中 @@ -146,20 +137,6 @@ public bool IsLogining } } } - // 登录 - public void Login(string username, string password, Action callback) - { - IsLogining = true; - App.Service.Login(username, password, delegate(string token, bool success, string error) - { - IsLogining = false; - - if (error == null) - App.Service.Token = token; - - callback(error); - }); - } // 登录钩子 public delegate void LoginChangedHandler(object sender, bool isLogin); public event LoginChangedHandler LoginChanged; diff --git a/sbbs-client-wp7.csproj b/sbbs-client-wp7.csproj index 91368bb..dd21cc7 100644 --- a/sbbs-client-wp7.csproj +++ b/sbbs-client-wp7.csproj @@ -68,6 +68,7 @@ BoardPage.xaml + LoginPage.xaml