Skip to content

Commit

Permalink
递归浏览文件夹ListBox
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Mar 8, 2012
1 parent d17a272 commit f762391
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 56 deletions.
11 changes: 2 additions & 9 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,15 @@ 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<ObservableCollection<BoardViewModel>>("Favorates", ViewModel.FavoratesDirectory.First.Value);
else
LocalCache.Set<ObservableCollection<BoardViewModel>>("Favorates", ViewModel.FavoratesItems);
LocalCache.Set<ObservableCollection<BoardViewModel>>("Favorates", ViewModel.FavoratesItems);
LocalCache.Set<ObservableCollection<TopicViewModel>>("Topten", ViewModel.ToptenItems);
}

// Code to execute when the application is closing (eg, user hit Back)
// 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<ObservableCollection<BoardViewModel>>("Favorates", ViewModel.FavoratesDirectory.First.Value);
else
LocalCache.Set<ObservableCollection<BoardViewModel>>("Favorates", ViewModel.FavoratesItems);
LocalCache.Set<ObservableCollection<BoardViewModel>>("Favorates", ViewModel.FavoratesItems);
LocalCache.Set<ObservableCollection<TopicViewModel>>("Topten", ViewModel.ToptenItems);
}

Expand Down
4 changes: 1 addition & 3 deletions Controls/ExtendedListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
64 changes: 64 additions & 0 deletions Controls/RecursiveListBox.cs
Original file line number Diff line number Diff line change
@@ -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<Sbbs.BoardViewModel>;
public class RecursiveListBox : ListBox
{
// 浏览历史
private Stack<CollectionType> history = new Stack<CollectionType>();

// 叶子结点点击事件
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;
}
}
}
2 changes: 1 addition & 1 deletion MailPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</ScrollViewer>
</StackPanel>

<ScrollViewer x:Name="Content" Visibility="Collapsed" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer x:Name="MailContent" Visibility="Collapsed" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="{Binding Content}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
</ScrollViewer>
<toolkit:PerformanceProgressBar x:Name="LoadProgress" IsIndeterminate="True" Grid.Row="1" />
Expand Down
2 changes: 1 addition & 1 deletion MailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
14 changes: 4 additions & 10 deletions MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -76,15 +77,8 @@
<controls:PanoramaItem.Header>
<TextBlock motion:MetroInMotion.Tilt="6" Tap="RefreshFavorates_Tap" Text="收藏夹" Foreground="{StaticResource PhoneAccentBrush}" FontSize="60"/>
</controls:PanoramaItem.Header>
<ListBox ItemsSource="{Binding FavoratesItems}" ItemTemplate="{StaticResource BoardDataTemplate}"
SelectionChanged="Favorates_Selected"
CacheMode="BitmapCache">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<custom:RecursiveListBox ItemsSource="{Binding FavoratesItems}" ItemTemplate="{StaticResource BoardDataTemplate}"
LeafItemTap="Favorates_Selected"/>
</controls:PanoramaItem>

<!-- 我的账户 -->
Expand Down
21 changes: 1 addition & 20 deletions MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -174,7 +156,6 @@ private void LoadFavorates()
if (error != null)
return;

App.ViewModel.FavoratesDirectory.Clear();
App.ViewModel.FavoratesItems = boards;
});
}
Expand Down
2 changes: 1 addition & 1 deletion SampleData/MainViewModelSampleData.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
4 changes: 2 additions & 2 deletions Sbbs/BoardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +16,7 @@ public class BoardViewModel : INotifyPropertyChanged
private uint count;
private bool leaf = true;
private ObservableCollection<BoardViewModel> boards;

[DataMember(Name = "name")]
public string EnglishName
{
Expand Down
2 changes: 1 addition & 1 deletion Sbbs/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Runtime.Serialization;
using System.Collections.ObjectModel;

namespace sbbs_client_wp7.Sbbs
namespace Sbbs
{
// 服务器响应通常会包括一个字段作为返回值,该返回值字段名通常不定,
// 因此响应DataContract子类需要自己手动定义哪个字段是返回值字段
Expand Down
2 changes: 1 addition & 1 deletion Sbbs/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;

namespace sbbs_client_wp7.Sbbs
namespace Sbbs
{
// 集合类型
using TopicCollection = ObservableCollection<TopicViewModel>;
Expand Down
2 changes: 1 addition & 1 deletion Sbbs/TopicViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sbbs/TopicsGroupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sbbs/UserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;

namespace sbbs_client_wp7.Sbbs
namespace Sbbs
{
public class UserViewModel : INotifyPropertyChanged
{
Expand Down
28 changes: 27 additions & 1 deletion Themes/generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ControlTemplate TargetType="custom:ExtendedListBox">
<ScrollViewer x:Name="ScrollViewer" Foreground="{TemplateBinding Foreground}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<StackPanel>
<ItemsPresenter x:Name="Content"/>
<ItemsPresenter/>
<TextBlock x:Name="LoadMore" Visibility="Collapsed" HorizontalAlignment="Center" Margin="0 5 0 25" Text="载入下一页..." Foreground="{StaticResource PhoneAccentBrush}" FontSize="{StaticResource PhoneFontSizeNormal}" />
</StackPanel>
</ScrollViewer>
Expand All @@ -33,4 +33,30 @@
</Setter>
</Style>

<Style TargetType="custom:RecursiveListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="custom:RecursiveListBox">
<ScrollViewer x:Name="ScrollViewer" Foreground="{TemplateBinding Foreground}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</Setter.Value>
</Setter>
</Style>

</ResourceDictionary>
3 changes: 0 additions & 3 deletions ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public MainViewModel()
// 初始化
CurrentBoard = new CurrentBoardViewModel();
CurrentTopic = new CurrentTopicViewModel();
FavoratesDirectory = new LinkedList<ObservableCollection<BoardViewModel>>();
}

// 十大热帖
Expand Down Expand Up @@ -97,8 +96,6 @@ public ObservableCollection<BoardViewModel> FavoratesItems
}
}
}
// --收藏夹浏览栈
public LinkedList<ObservableCollection<BoardViewModel>> FavoratesDirectory { get; set; }

// 当前版面
public CurrentBoardViewModel CurrentBoard { get; set; }
Expand Down
1 change: 1 addition & 0 deletions sbbs-client-wp7.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<DependentUpon>BoardSettingsPage.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\ExtendedListBox.cs" />
<Compile Include="Controls\RecursiveListBox.cs" />
<Compile Include="Controls\UIPropertyMetadata.cs" />
<Compile Include="DynamicOrientationChanges\AnimateOrientationChangesFrame.cs" />
<Compile Include="DynamicOrientationChanges\FadeOrientationChangesFrame.cs" />
Expand Down

0 comments on commit f762391

Please sign in to comment.