如何排序在Windows Phone的一个LongListSelector [英] How to Sort a LongListSelector in Windows Phone

查看:169
本文介绍了如何排序在Windows Phone的一个LongListSelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够绑定到我LongListSelector数据无论是在升序或降序排序。我有麻烦排序的数据绑定到我的LongListSelector。本来没有试图实现一种我的解决方案是工作,但我相信排序参与时,我失去了一些东西。我也曾尝试如何排序使用CollectionViewSource <一LongListSelector / A>没有运气。什么是排序LongListSelector的最佳方式?



MainPage.xaml中



 <电话:LongListSelector X:NAME =最近的保证金=0,0,0,72
LayoutMode =网格GridCellSize =108,108
的SelectionChanged =recent_SelectionChanged>



MainPage.xaml.cs中(OLD)

 保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
Recent.ItemsSource = App.PictureList.Pictures; //作品!

如果(Settings.AscendingSort.Value)
{
//Recent.ItemsSource = App.PictureList.Pictures.OrderBy(X => x.DateTaken); //错误,说明无法转换implicityly类型'SYstem.Linq.IOrderedEnumerable到System.Collections.IList
Recent.ItemsSource = App.PictureList.Pictures.OrderBy(X => x.DateTaken)作为System.Collections.IList ;显示//没有错误,但没有
}
,否则
{
//Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(X => x.DateTaken);
Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(X => x.DateTaken)为System.Collections.IList;
}
}



**编辑



MainPage.xaml.cs中(NEW)

 保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
//Recent.ItemsSource = App.PictureList.Pictures; //工程与删除,排序。如果

(Settings.AscendingSort.Value)
{
//Recent.ItemsSource = App.PictureList.Pictures.OrderBy(X => x.DateTaken).ToList( );
//Recent.ItemsSource =新的ObservableCollection< Models.Picture>(App.PictureList.Pictures.OrderBy(X => x.DateTaken));
App.PictureList.Pictures =新的ObservableCollection< Models.Picture>(App.PictureList.Pictures.OrderBy(X => x.DateTaken)); //错误
Recent.ItemsSource = App.PictureList.Pictures;
}
,否则
{
//Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(X => x.DateTaken).ToList();
//Recent.ItemsSource =新的ObservableCollection< Models.Picture>(App.PictureList.Pictures.OrderByDescending(X => x.DateTaken));
App.PictureList.Pictures =新的ObservableCollection< Models.Picture>(App.PictureList.Pictures.OrderByDescending(X => x.DateTaken)); //错误
Recent.ItemsSource = App.PictureList.Pictures;
}



App.xaml.cs

 公共静态PictureRepository PictureList 
{
得到
{
返回PictureRepository.Instance;
}
}



PictureRepository.cs

 #区域常数

公共常量字符串IsolatedStoragePath =图片;

#endregion

#地区的字段

//私人只读的ObservableCollection<图片和GT; _pictures =新的ObservableCollection<图片和GT;();
私人的ObservableCollection<图片和GT; _pictures =新的ObservableCollection<图片和GT;();

#endregion

#区域属性

公众的ObservableCollection<图片和GT;图片
{
// {返回_pictures; }
{返回_pictures; }
集合{新的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));
// VAR pictureFiles = isoFile.GetFileNames(Path.Combine(IsolatedStoragePath,));

//通过迭代列表中的所有图片文件,每个使用先前创建
的foreach(在pictureFiles PictureFile的变种)
的LoadFromLocalStorage加载{
变种图片= LoadFromLocalStorage(PictureFile的,IsolatedStoragePath);
_pictures.Add(图片);
}
}


解决方案

替换
Recent.ItemsSource = App.PictureList.Pictures.OrderBy(X => x.DateTaken)作为System.Collections.IList;




Recent.ItemsSource = App.PictureList.Pictures.OrderBy(X => x.DateTaken).ToList()



当您使用排序依据它返回一个IEnumerable的不是一个列表中,使 App.PictureList.Pictures.OrderBy(X => x.DateTaken)为System.Collections.IList 只会返回null


I would like to be able to sort the data bound to my LongListSelector either in ascending or descending order. I am having trouble binding the sorted data to my LongListSelector. Originally without trying to implement a sort my solution was working, but I believe that I am missing something when sorting is involved. I have also tried How to Sort a LongListSelector using CollectionViewSource with no luck. What is the best way to sort a LongListSelector?

MainPage.xaml

<phone:LongListSelector x:Name="Recent" Margin="0,0,0,72"
                                    LayoutMode="Grid" GridCellSize="108,108" 
                                    SelectionChanged="recent_SelectionChanged">

MainPage.xaml.cs (OLD)

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Recent.ItemsSource = App.PictureList.Pictures;  //works!

        if (Settings.AscendingSort.Value)
        {
            //Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken); //Error stating Cannot implicityly convert type 'SYstem.Linq.IOrderedEnumerable to System.Collections.IList
            Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken) as System.Collections.IList; //No error but nothing is displayed
        }
        else
        {                
            //Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(x => x.DateTaken);
            Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(x => x.DateTaken) as System.Collections.IList;
        }
    }

**EDIT

MainPage.xaml.cs (NEW)

protected override void OnNavigatedTo(NavigationEventArgs e)
    {   
        //Recent.ItemsSource = App.PictureList.Pictures; //Works with deleting, not sorted.

        if (Settings.AscendingSort.Value)
        {
            //Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken).ToList();
            //Recent.ItemsSource = new ObservableCollection<Models.Picture>(App.PictureList.Pictures.OrderBy(x => x.DateTaken));
            App.PictureList.Pictures = new ObservableCollection<Models.Picture>(App.PictureList.Pictures.OrderBy(x => x.DateTaken)); //Error 
            Recent.ItemsSource = App.PictureList.Pictures;
        }
        else
        {
            //Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(x => x.DateTaken).ToList();
            //Recent.ItemsSource = new ObservableCollection<Models.Picture>(App.PictureList.Pictures.OrderByDescending(x => x.DateTaken));
            App.PictureList.Pictures = new ObservableCollection<Models.Picture>(App.PictureList.Pictures.OrderByDescending(x => x.DateTaken)); //Error 
            Recent.ItemsSource = App.PictureList.Pictures;
        }

App.xaml.cs

public static PictureRepository PictureList
    {
        get
        {
            return PictureRepository.Instance;
        }
    }

PictureRepository.cs

#region Constants

    public const string IsolatedStoragePath = "Pictures";

    #endregion

    #region Fields

    //private readonly ObservableCollection<Picture> _pictures = new ObservableCollection<Picture>();
    private ObservableCollection<Picture> _pictures = new ObservableCollection<Picture>();

    #endregion

    #region Properties

    public ObservableCollection<Picture> Pictures
    {
        //get { return _pictures; }
        get { return _pictures; }
        set{ new ObservableCollection<Picture>(_pictures); }
    }

    #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 pictureFiles = isoFile.GetFileNames(Path.Combine(IsolatedStoragePath, ""));

        //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);
        }
    }

解决方案

Replace Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken) as System.Collections.IList;

by Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken).ToList()

When you use OrderBy it return a IEnumerable not a List so App.PictureList.Pictures.OrderBy(x => x.DateTaken) as System.Collections.IList would just return null

这篇关于如何排序在Windows Phone的一个LongListSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆