From f762391a9ac7410f6812ea8af6cb37ba89a7542d Mon Sep 17 00:00:00 2001 From: Zhao Cheng Date: Fri, 9 Mar 2012 00:26:48 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=92=E5=BD=92=E6=B5=8F=E8=A7=88=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9ListBox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 11 +---- Controls/ExtendedListBox.cs | 4 +- Controls/RecursiveListBox.cs | 64 +++++++++++++++++++++++++ MailPage.xaml | 2 +- MailPage.xaml.cs | 2 +- MainPage.xaml | 14 ++---- MainPage.xaml.cs | 21 +------- SampleData/MainViewModelSampleData.xaml | 2 +- Sbbs/BoardViewModel.cs | 4 +- Sbbs/Response.cs | 2 +- Sbbs/Service.cs | 2 +- Sbbs/TopicViewModel.cs | 2 +- Sbbs/TopicsGroupViewModel.cs | 2 +- Sbbs/UserViewModel.cs | 2 +- Themes/generic.xaml | 28 ++++++++++- ViewModels/MainViewModel.cs | 3 -- sbbs-client-wp7.csproj | 1 + 17 files changed, 110 insertions(+), 56 deletions(-) create mode 100644 Controls/RecursiveListBox.cs diff --git a/App.xaml.cs b/App.xaml.cs index cfc4fd9..963004f 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -107,11 +107,7 @@ private void Application_Activated(object sender, ActivatedEventArgs e) // This code will not execute when the application is closing private void Application_Deactivated(object sender, DeactivatedEventArgs e) { - // Ensure that required application state is persisted here. - if (ViewModel.FavoratesDirectory.First != null) - LocalCache.Set>("Favorates", ViewModel.FavoratesDirectory.First.Value); - else - LocalCache.Set>("Favorates", ViewModel.FavoratesItems); + LocalCache.Set>("Favorates", ViewModel.FavoratesItems); LocalCache.Set>("Topten", ViewModel.ToptenItems); } @@ -119,10 +115,7 @@ private void Application_Deactivated(object sender, DeactivatedEventArgs e) // This code will not execute when the application is deactivated private void Application_Closing(object sender, ClosingEventArgs e) { - if (ViewModel.FavoratesDirectory.First != null) - LocalCache.Set>("Favorates", ViewModel.FavoratesDirectory.First.Value); - else - LocalCache.Set>("Favorates", ViewModel.FavoratesItems); + LocalCache.Set>("Favorates", ViewModel.FavoratesItems); LocalCache.Set>("Topten", ViewModel.ToptenItems); } diff --git a/Controls/ExtendedListBox.cs b/Controls/ExtendedListBox.cs index 7e3f07f..f0ec831 100644 --- a/Controls/ExtendedListBox.cs +++ b/Controls/ExtendedListBox.cs @@ -6,13 +6,11 @@ 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.Windows.Controls.Primitives; using System.Collections; using System.Collections.Specialized; +using System.Windows.Media; namespace CustomControls { diff --git a/Controls/RecursiveListBox.cs b/Controls/RecursiveListBox.cs new file mode 100644 index 0000000..f6b1c25 --- /dev/null +++ b/Controls/RecursiveListBox.cs @@ -0,0 +1,64 @@ +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.Controls.Primitives; +using System.Collections; +using System.Collections.Specialized; +using Microsoft.Phone.Controls; +using System.Collections.ObjectModel; + +namespace CustomControls +{ + using ElementType = Sbbs.BoardViewModel; + using CollectionType = ObservableCollection; + public class RecursiveListBox : ListBox + { + // 浏览历史 + private Stack history = new Stack(); + + // 叶子结点点击事件 + public delegate void OnLeafItemTap(object sender, SelectionChangedEventArgs e); + public event OnLeafItemTap LeafItemTap; + + // 捕捉选择事件 + private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (e.AddedItems.Count == 1) + { + // 清除选择 + (sender as ListBox).SelectedIndex = -1; + + ElementType board = e.AddedItems[0] as ElementType; + if (board.Leaf && LeafItemTap != null) + { + // 叶子结点直接触发事件 + LeafItemTap(sender, e); + } + else if (!board.Leaf) + { + // 非叶子结点切换视角 + if (board.EnglishName == "..") + { + ItemsSource = history.Pop() as CollectionType; + } + else + { + history.Push(ItemsSource as CollectionType); + ItemsSource = board.Boards; + } + } + } + } + + public RecursiveListBox() + { + DefaultStyleKey = typeof(RecursiveListBox); + SelectionChanged += ListBox_SelectionChanged; + } + } +} diff --git a/MailPage.xaml b/MailPage.xaml index 420e204..c3a0d55 100644 --- a/MailPage.xaml +++ b/MailPage.xaml @@ -52,7 +52,7 @@ - + diff --git a/MailPage.xaml.cs b/MailPage.xaml.cs index a9387d9..6e96f50 100644 --- a/MailPage.xaml.cs +++ b/MailPage.xaml.cs @@ -39,7 +39,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { LoadProgress.Visibility = Visibility.Collapsed; LoadProgress.IsIndeterminate = false; - Content.Visibility = Visibility.Visible; + MailContent.Visibility = Visibility.Visible; if (mail != null) App.ViewModel.Mail.Content = mail.Content; }); diff --git a/MainPage.xaml b/MainPage.xaml index 1c88d36..25469e4 100644 --- a/MainPage.xaml +++ b/MainPage.xaml @@ -2,7 +2,8 @@ x:Class="sbbs_client_wp7.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:sbbs_client_wp7" - xmlns:sbbs="clr-namespace:sbbs_client_wp7.Sbbs" + xmlns:custom="clr-namespace:CustomControls" + xmlns:sbbs="clr-namespace:Sbbs" xmlns:motion="clr-namespace:MetroInMotionUtils" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" @@ -76,15 +77,8 @@ - - - - - + diff --git a/MainPage.xaml.cs b/MainPage.xaml.cs index 38c11aa..d99b312 100644 --- a/MainPage.xaml.cs +++ b/MainPage.xaml.cs @@ -123,27 +123,9 @@ private void Favorates_Selected(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count == 1) { - ListBox list = sender as ListBox; BoardViewModel board = e.AddedItems[0] as BoardViewModel; - - // 收藏夹目录 - if (board.Leaf != true && board.EnglishName != "..") - { - App.ViewModel.FavoratesDirectory.AddLast(App.ViewModel.FavoratesItems); - App.ViewModel.FavoratesItems = board.Boards; - return; - } - else if (board.EnglishName == "..") - { - App.ViewModel.FavoratesItems = App.ViewModel.FavoratesDirectory.Last.Value; - App.ViewModel.FavoratesDirectory.RemoveLast(); - return; - } - + this.NavigationService.Navigate(new Uri("/BoardPage.xaml?board=" + board.EnglishName + "&description=" + board.Description, UriKind.Relative)); - - // 清除选择,否则同样的项目无法点击第二次 - (sender as ListBox).SelectedIndex = -1; } } @@ -174,7 +156,6 @@ private void LoadFavorates() if (error != null) return; - App.ViewModel.FavoratesDirectory.Clear(); App.ViewModel.FavoratesItems = boards; }); } diff --git a/SampleData/MainViewModelSampleData.xaml b/SampleData/MainViewModelSampleData.xaml index 0760ea5..731d6e8 100644 --- a/SampleData/MainViewModelSampleData.xaml +++ b/SampleData/MainViewModelSampleData.xaml @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:sbbs_client_wp7" - xmlns:sbbs="clr-namespace:sbbs_client_wp7.Sbbs" + xmlns:sbbs="clr-namespace:Sbbs" IsDataLoaded="True" IsLogin="False" IsLogining="False"> diff --git a/Sbbs/BoardViewModel.cs b/Sbbs/BoardViewModel.cs index 3b0bc43..2940362 100644 --- a/Sbbs/BoardViewModel.cs +++ b/Sbbs/BoardViewModel.cs @@ -4,7 +4,7 @@ using System.Runtime.Serialization.Json; using System.Runtime.Serialization; -namespace sbbs_client_wp7.Sbbs +namespace Sbbs { [DataContract(Name = "board")] public class BoardViewModel : INotifyPropertyChanged @@ -16,7 +16,7 @@ public class BoardViewModel : INotifyPropertyChanged private uint count; private bool leaf = true; private ObservableCollection boards; - + [DataMember(Name = "name")] public string EnglishName { diff --git a/Sbbs/Response.cs b/Sbbs/Response.cs index 5d252c7..008365e 100644 --- a/Sbbs/Response.cs +++ b/Sbbs/Response.cs @@ -2,7 +2,7 @@ using System.Runtime.Serialization; using System.Collections.ObjectModel; -namespace sbbs_client_wp7.Sbbs +namespace Sbbs { // 服务器响应通常会包括一个字段作为返回值,该返回值字段名通常不定, // 因此响应DataContract子类需要自己手动定义哪个字段是返回值字段 diff --git a/Sbbs/Service.cs b/Sbbs/Service.cs index ffe8c25..cdd13fd 100644 --- a/Sbbs/Service.cs +++ b/Sbbs/Service.cs @@ -7,7 +7,7 @@ using System.Runtime.Serialization.Json; using System.Runtime.Serialization; -namespace sbbs_client_wp7.Sbbs +namespace Sbbs { // 集合类型 using TopicCollection = ObservableCollection; diff --git a/Sbbs/TopicViewModel.cs b/Sbbs/TopicViewModel.cs index 92d4f8b..79dfbe1 100644 --- a/Sbbs/TopicViewModel.cs +++ b/Sbbs/TopicViewModel.cs @@ -4,7 +4,7 @@ using System.Runtime.Serialization.Json; using System.Runtime.Serialization; -namespace sbbs_client_wp7.Sbbs +namespace Sbbs { [DataContract(Name = "topic")] public class TopicViewModel : INotifyPropertyChanged diff --git a/Sbbs/TopicsGroupViewModel.cs b/Sbbs/TopicsGroupViewModel.cs index d3e1049..7480fcd 100644 --- a/Sbbs/TopicsGroupViewModel.cs +++ b/Sbbs/TopicsGroupViewModel.cs @@ -4,7 +4,7 @@ using System.Runtime.Serialization.Json; using System.Runtime.Serialization; -namespace sbbs_client_wp7.Sbbs +namespace Sbbs { [DataContract] public class HotTopicsViewModel : INotifyPropertyChanged diff --git a/Sbbs/UserViewModel.cs b/Sbbs/UserViewModel.cs index 9540bb4..dfdb4e8 100644 --- a/Sbbs/UserViewModel.cs +++ b/Sbbs/UserViewModel.cs @@ -4,7 +4,7 @@ using System.Runtime.Serialization.Json; using System.Runtime.Serialization; -namespace sbbs_client_wp7.Sbbs +namespace Sbbs { public class UserViewModel : INotifyPropertyChanged { diff --git a/Themes/generic.xaml b/Themes/generic.xaml index 67c4ea0..be899e8 100644 --- a/Themes/generic.xaml +++ b/Themes/generic.xaml @@ -17,7 +17,7 @@ - + @@ -33,4 +33,30 @@ + + + + + \ No newline at end of file diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index a8d37ec..468156d 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -28,7 +28,6 @@ public MainViewModel() // 初始化 CurrentBoard = new CurrentBoardViewModel(); CurrentTopic = new CurrentTopicViewModel(); - FavoratesDirectory = new LinkedList>(); } // 十大热帖 @@ -97,8 +96,6 @@ public ObservableCollection FavoratesItems } } } - // --收藏夹浏览栈 - public LinkedList> FavoratesDirectory { get; set; } // 当前版面 public CurrentBoardViewModel CurrentBoard { get; set; } diff --git a/sbbs-client-wp7.csproj b/sbbs-client-wp7.csproj index d5b9e2d..3498b97 100644 --- a/sbbs-client-wp7.csproj +++ b/sbbs-client-wp7.csproj @@ -72,6 +72,7 @@ BoardSettingsPage.xaml +