2

我有一个LongListSelector当前正在填充来自IsolatedStorage. 我希望能够按日期升序或降序对这些进行排序。我一直在参考http://babaandthepigman.wordpress.com/2011/07/03/wp7-collectionviewsource-sorting-a-listbox/寻求帮助。出于某种原因,我无法将ItemsSourcemy绑定LongListSelectorCollectionViewSource以实现排序功能。

PictureRepository.cs(从IsolatedStorage应用程序启动时加载图片)

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

应用程序.xaml.cs

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

主页.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)); 

        }
    }
4

1 回答 1

1

如何在 Windows Phone中对 LongListSelector 进行排序是最好和最简单的解决方案。稍微改变了我的实现,但效果很好。

于 2013-09-15T18:31:40.773 回答