问题描述
我有一个 LongListSelector
当前正在填充图片来自 IsolatedStorage
。我希望能够通过日期升序或降序排序这些。我一直在引用寻求帮助。出于某种原因,我有麻烦绑定的ItemsSource
我的 LongListSelector
到 CollectionViewSource
来实现排序功能。
PictureRepository.cs(从 IsolatedStorage
应用程序启动时)
#区域常数
公共常量字符串IsolatedStoragePath =图片;
#endregion
#区域属性
公众的ObservableCollection<图片和GT;图片
{
获得;
私人集;
}
#endregion
#地区Singleton模式
私人PictureRepository()
{
LoadAllPicturesFromIsolatedStorage( );
}
公共静态只读PictureRepository实例=新PictureRepository();
#endregion
///<总结>
///保存到本地存储
///这个方法有两个参数:捕获图像实例,并在独立存储
图片文件夹的名称///< /总结> ;
///< PARAM NAME =capturedPicture>< /参数>
///< PARAM NAME =目录>< /参数>
公共无效SaveToLocalStorage(CapturedPicture capturedPicture,串目录)
{
//调用IsolatedStorageFile.GetUserStoreForApplication得到一个独立的存储文件
VAR isoFile = IsolatedStorageFile.GetUserStoreForApplication();
//调用位于公用IsolatedStorageFileExtensions类IsolatedStorageFile.EnsureDirectory扩展方法,以确认照片文件夹存在。
isoFile.EnsureDirectory(目录);
//合并图像文件夹,并捕获图像文件名,并使用该路径来创建新文件
串文件路径= Path.Combine(目录,capturedPicture.FileName);使用
(VAR FILESTREAM = isoFile.CreateFile(文件路径))
{
使用(VAR作家=新的BinaryWriter(FILESTREAM))
{
capturedPicture.Serialize(作家);
}
}
}
///<总结>
///要加载所有保存的图片,并将它们添加到图片列表页
///< /总结>
公共CapturedPicture LoadFromLocalStorage(字符串文件名,目录字符串)
{
//要打开文件,添加到IsolatedStorageFile.GetUserStoreForApplication
VAR isoFile = IsolatedStorageFile.GetUserStoreForApplication()的调用;
//结合的目录和文件名
字符串文件路径= Path.Combine(目录,文件名);
//使用路径通过IsolatedStorageFile.OpenFile方法
使用(VAR FILESTREAM = isoFile.OpenFile(文件路径,FileMode.Open,FileAccess.Read))$打开从独立存储的图片文件b $ b {
//使用反序列化CapturedPicture例如
(VAR读者=新BinaryReader(FILESTREAM))
{
变种capturedPicture =新CapturedPicture创建一个BinaryReader实例() ;
//创建一个名为CapturedPicture.Deserialize反序列化捕获的图像,并返回它的类型CapturedPicture的新实例
capturedPicture.Deserialize(读卡器);
返回capturedPicture;
}
}
}
///<总结>
///要加载在启动时
///<的所有图片; /总结>
私人无效LoadAllPicturesFromIsolatedStorage()
{
//添加调用IsolatedStorageFile.GetUserStoreForApplication打开一个独立的存储文件
VAR isoFile = IsolatedStorageFile.GetUserStoreForApplication();
//调用位于公用IsolatedStorageFileExtensions类IsolatedStorageFile.EnsureDirectory扩展方法,以确认照片文件夹存在
isoFile.EnsureDirectory(IsolatedStoragePath);
//调用使用的图片目录,* .JPG的IsolatedStorageFile.GetFileNames作为过滤器来获取所有保存的图片
VAR pictureFiles = isoFile.GetFileNames(Path.Combine(IsolatedStoragePath,* .JPG));
变种照片=新的List<图片和GT;();
//通过迭代列表中的所有图片文件,每个使用先前创建
的foreach(在pictureFiles PictureFile的变种)
的LoadFromLocalStorage加载{
变种图片= LoadFromLocalStorage(PictureFile的,IsolatedStoragePath);
pictures.Add(图片);
}
图片=新的ObservableCollection<图片和GT;(pictures.OrderBy(X => x.DateTaken));
}
App.xaml.cs
公共静态PictureRepository PictureList
{
得到
{
返回PictureRepository.Instance;
}
}
MainPage.xaml中
<电话:LongListSelector X:NAME =最近的保证金=0,0,0,72
LayoutMode =网格GridCellSize = 108,108
的SelectionChanged =recent_SelectionChanged>
MainPage.xaml.cs中
#地区的字段
公共System.Windows.Data.CollectionViewSource来源{搞定;组; }
#endregion
保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
//Recent.ItemsSource = App.PictureList.Pictures; //工作,未分类的
来源=新System.Windows.Data.CollectionViewSource();
Source.Source = App.PictureList.Pictures; //不工作
//需要某种形式的铸造到IList的LongListSelector?
Recent.ItemsSource = Source.View作为IList的< ??>();
如果(Settings.AscendingSort.Value)
{
//Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderBy(X => x.DateTaken);
//Recent.ItemsSource = App.PictureList.Pictures.OrderBy(X => x.DateTaken);
Source.SortDescriptions.Clear();
Source.SortDescriptions.Add(新SortDescription(日期,ListSortDirection.Ascending));
}
,否则
{
//Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderByDescending(X => x.DateTaken);
//Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(X => x.DateTaken);
Source.SortDescriptions.Clear();
Source.SortDescriptions.Add(新SortDescription(日期,ListSortDirection.Descending));
}
}
是最好,最简单的解决方案。稍微改变了我的实现,但它的伟大工程。
I have a LongListSelector
that is currently being populated with images from IsolatedStorage
. I would like be able to sort these by date in ascending or descending order. I have been referencing http://babaandthepigman.wordpress.com/2011/07/03/wp7-collectionviewsource-sorting-a-listbox/ for assistance. For some reason, I have having trouble binding the ItemsSource
of my LongListSelector
to the CollectionViewSource
to implement the sort feature.
PictureRepository.cs (to load the pictures from IsolatedStorage
when application starts)
#region Constants
public const string IsolatedStoragePath = "Pictures";
#endregion
#region Properties
public ObservableCollection<Picture> Pictures
{
get;
private set;
}
#endregion
#region Singleton Pattern
private PictureRepository()
{
LoadAllPicturesFromIsolatedStorage();
}
public static readonly PictureRepository Instance = new PictureRepository();
#endregion
/// <summary>
/// Saves to local storage
/// This method gets two parameters: the captured picture instance and the name of the pictures folder in the isolated storage
/// </summary>
/// <param name="capturedPicture"></param>
/// <param name="directory"></param>
public void SaveToLocalStorage(CapturedPicture capturedPicture, string directory)
{
//call IsolatedStorageFile.GetUserStoreForApplication to get an isolated storage file
var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
//Call the IsolatedStorageFile.EnsureDirectory extension method located in the Common IsolatedStorageFileExtensions class to confirm that the pictures folder exists.
isoFile.EnsureDirectory(directory);
//Combine the pictures folder and captured picture file name and use this path to create a new file
string filePath = Path.Combine(directory, capturedPicture.FileName);
using (var fileStream = isoFile.CreateFile(filePath))
{
using (var writer = new BinaryWriter(fileStream))
{
capturedPicture.Serialize(writer);
}
}
}
/// <summary>
/// To load all saved pictures and add them to the pictures list page
/// </summary>
public CapturedPicture LoadFromLocalStorage(string fileName, string directory)
{
//To open the file, add a call to the IsolatedStorageFile.GetUserStoreForApplication
var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
//Combine the directory and file name
string filePath = Path.Combine(directory, fileName);
//use the path to open the picture file from the isolated storage by using the IsolatedStorageFile.OpenFile method
using (var fileStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
{
//create a BinaryReader instance for deserializing the CapturedPicture instance
using (var reader = new BinaryReader(fileStream))
{
var capturedPicture = new CapturedPicture();
//create a new instance of the type CapturedPicture called CapturedPicture.Deserialize to deserialize the captured picture and return it
capturedPicture.Deserialize(reader);
return capturedPicture;
}
}
}
/// <summary>
/// To load all the pictures at start time
/// </summary>
private void LoadAllPicturesFromIsolatedStorage()
{
//add call to the IsolatedStorageFile.GetUserStoreForApplication to open an isolated storage file
var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
//Call the IsolatedStorageFile.EnsureDirectory extension method located in the Common IsolatedStorageFileExtensions class to confirm that the pictures folder exists
isoFile.EnsureDirectory(IsolatedStoragePath);
//Call the IsolatedStorageFile.GetFileNames using the pictures directory and *.jpg as a filter to get all saved pictures
var pictureFiles = isoFile.GetFileNames(Path.Combine(IsolatedStoragePath, "*.jpg"));
var pictures = new List<Picture>();
//Iterate through all the picture files in the list and load each using the LoadFromLocalStorage you created earlier
foreach (var pictureFile in pictureFiles)
{
var picture = LoadFromLocalStorage(pictureFile, IsolatedStoragePath);
pictures.Add(picture);
}
Pictures = new ObservableCollection<Picture>(pictures.OrderBy(x => x.DateTaken));
}
App.xaml.cs
public static PictureRepository PictureList
{
get
{
return PictureRepository.Instance;
}
}
MainPage.xaml
<phone:LongListSelector x:Name="Recent" Margin="0,0,0,72"
LayoutMode="Grid" GridCellSize="108,108"
SelectionChanged="recent_SelectionChanged">
MainPage.xaml.cs
#region Fields
public System.Windows.Data.CollectionViewSource Source { get; set; }
#endregion
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//Recent.ItemsSource = App.PictureList.Pictures; //working, unsorted
Source = new System.Windows.Data.CollectionViewSource();
Source.Source = App.PictureList.Pictures; //Not Working
//Need some sort of Cast to IList for LongListSelector?
Recent.ItemsSource = Source.View as IList<??>();
if (Settings.AscendingSort.Value)
{
//Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderBy(x => x.DateTaken);
//Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken);
Source.SortDescriptions.Clear();
Source.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Ascending));
}
else
{
//Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderByDescending(x => x.DateTaken);
//Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(x => x.DateTaken);
Source.SortDescriptions.Clear();
Source.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Descending));
}
}
How to Sort a LongListSelector in Windows Phone was the best and most simple solution. Slightly changed my implementation but it works great.
这篇关于如何排序使用CollectionViewSource一个LongListSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!