c# - 如何在 Windows Phone 中对 LongListSelector 进行排序

标签 c# windows-phone-7 sorting windows-phone-8 longlistselector

我希望能够按升序或降序对绑定(bind)到我的 LongListSelector 的数据进行排序。我无法将已排序的数据绑定(bind)到我的 LongListSelector。最初没有尝试实现排序我的解决方案是有效的,但我相信在涉及排序时我遗漏了一些东西。我也试过How to Sort a LongListSelector using CollectionViewSource没有运气。对 LongListSelector 进行排序的最佳方法是什么?

主页.xaml

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

MainPage.xaml.cs(旧)

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

**编辑

MainPage.xaml.cs(新)

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

图片库.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);
    }
}

最佳答案

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

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

当您使用 OrderBy 时,它返回一个 IEnumerable 而不是一个列表,所以 App.PictureList.Pictures.OrderBy(x => x.DateTaken) as System.Collections.IList 只会返回 null

关于c# - 如何在 Windows Phone 中对 LongListSelector 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18815570/

相关文章:

c# - 如何在表达式中获取引用类型的值?

windows-phone-7 - 在 Windows Phone 7 中访问网站内容

windows-phone-7 - 如何通过外部事件停止 WP7 上的计时器?

javascript - JavaScript 中的排序

java - 将快速排序修改为使用枢轴的快速排序 'median of three'

java - 如何对由 "null"和字符串组合的数组进行排序?

c# - 将 web api 与 graphql 结合使用的最佳实践

c# - 对于复杂的搜索查询,Google 搜索返回 503 错误

c# - 在 C# 中调用基本构造函数

windows-phone-7 - 本地化 Windows Phone 7 应用程序