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

2024-02-14

我希望能够按升序或降序对绑定到 LongListSelector 的数据进行排序。我无法将排序后的数据绑定到我的 LongListSelector。最初,我的解决方案没有尝试实现排序,而是有效的,但我相信在涉及排序时我遗漏了一些东西。我也尝试过如何使用 CollectionViewSource 对 LongListSelector 进行排序 https://stackoverflow.com/questions/18709361/how-to-sort-a-longlistselector-using-collectionviewsource?noredirect=1#comment27573580_18709361没有运气。对 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;
    }
}

**EDIT

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

应用程序.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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Windows Phone 中对 LongListSelector 进行排序 的相关文章

  • Tensorflow 中的自定义资源

    由于某些原因 我需要为 Tensorflow 实现自定义资源 我试图从查找表实现中获得灵感 如果我理解得好的话 我需要实现3个TF操作 创建我的资源 资源的初始化 例如 在查找表的情况下填充哈希表 执行查找 查找 查询步骤 为了促进实施 我
  • MEX 文件中的断言导致 Matlab 崩溃

    我正在使用mxAssert 宏定义为matrix h在我的 C 代码中 mex 可以完美编译 当我调用的 mex 代码中违反断言时 该断言不会导致我的程序崩溃 而是导致 Matlab 本身崩溃 我错过了什么吗 这是有意的行为吗 当我查看 M
  • 添加对共享类的多个 WCF 服务的服务引用

    我正在尝试将我的 WCF Web 服务拆分为几个服务 而不是一个巨大的服务 但是 Visual Studio Silverlight 客户端 复制了两个服务共享的公共类 这是一个简单的例子来说明我的问题 在此示例中 有两个服务 两者都返回类
  • 通信对象 System.ServiceModel.Channels.ServiceChannel 不能用于通信

    通信对象System ServiceModel Channels ServiceChannel 无法用于通信 因为它处于故障状态 这个错误到底是什么意思 我该如何解决它 您收到此错误是因为您让服务器端发生 NET 异常 并且您没有捕获并处理
  • 处理 fanart.tv Web 服务响应 JSON 和 C#

    我正在尝试使用 fanart tv Webservice API 但有几个问题 我正在使用 Json Net Newtonsoft Json 并通过其他 Web 服务将 JSON 响应直接反序列化为 C 对象 这里的问题是元素名称正在更改
  • 使用实体框架从集合中删除项目

    我正在使用DDD 我有一个 Product 类 它是一个聚合根 public class Product IAggregateRoot public virtual ICollection
  • Guid 应包含 32 位数字和 4 个破折号

    我有一个包含 createuserwizard 控件的网站 创建帐户后 验证电子邮件及其验证 URL 将发送到用户的电子邮件地址 但是 当我进行测试运行时 单击电子邮件中的 URL 时 会出现以下错误 Guid should contain
  • TextBox 焦点的 WinForms 事件?

    我想添加一个偶数TextBox当它有焦点时 我知道我可以用一个简单的方法来做到这一点textbox1 Focus并检查布尔值 但我不想那样做 我想这样做 this tGID Focus new System EventHandler thi
  • 在 C# 中将位从 ulong 复制到 long

    所以看来 NET 性能计数器类型 http msdn microsoft com en us library system diagnostics performancecounter aspx有一个恼人的问题 它暴露了long对于计数器
  • 为什么 FTPWebRequest 或 WebRequest 通常不接受 /../ 路径?

    我正在尝试从 ftp Web 服务器自动执行一些上传 下载任务 当我通过客户端甚至通过 Firefox 连接到服务器时 为了访问我的目录 我必须指定如下路径 ftp ftpserver com AB00000 incoming files
  • 范围和临时初始化列表

    我试图将我认为是纯右值的内容传递到范围适配器闭包对象中 除非我将名称绑定到初始值设定项列表并使其成为左值 否则它不会编译 这里发生了什么 include
  • “MyClass”的类型初始值设定项引发异常

    以下是我的Windows服务代码 当我调试代码时 我收到错误 异常 CSMessageUtility CSDetails 的类型初始值设定项引发异常 using System using System Collections Generic
  • 如何排列表格中的项目 - MVC3 视图 (Index.cshtml)

    我想使用 ASP NET MVC3 显示特定类型食品样本中存在的不同类型维生素的含量 如何在我的视图 Index cshtml 中显示它 an example 这些是我的代码 table tr th th foreach var m in
  • UWP 无法在两个应用程序之间创建本地主机连接

    我正在尝试在两个 UWP 应用程序之间设置 TCP 连接 当服务器和客户端在同一个应用程序中运行时 它可以正常工作 但是 当我将服务器部分移动到一个应用程序并将客户端部分移动到另一个应用程序时 ConnectAsync 会引发异常 服务器未
  • C# 搜索目录中包含字符串的所有文件,然后返回该字符串

    使用用户在文本框中输入的内容 我想搜索目录中的哪个文件包含该文本 然后我想解析出信息 但我似乎找不到该字符串或至少返回信息 任何帮助将不胜感激 我当前的代码 private void btnSearchSerial Click object
  • Windows Phone 8 错误 - 应用程序安装失败

    我正在开发一个 Windows Phone 8 项目 我们通过 HockeyApp 将其部署为公司应用程序 我有一个从我们的赛门铁克证书生成的 PFX 文件 并且设备上安装了正确的 aetx 文件 如果我获取打算部署的 XAP 文件并将其复
  • 如何在 GCC 5 中处理双 ABI?

    我尝试了解如何克服 GCC 5 中引入的双重 ABI 的问题 但是 我没能做到 这是一个重现错误的非常简单的示例 我使用的GCC版本是5 2 如您所见 我的主要函数 在 main cpp 文件中 非常简单 main cpp include
  • boost::program_options:带有固定和可变标记的参数?

    是否可以在 boost program options 中使用此类参数 program p1 123 p2 234 p3 345 p12 678 即 是否可以使用第一个标记指定参数名称 例如 p 后跟一个数字 是动态的吗 我想避免这种情况
  • Swagger 为 ASP.CORE 3 中的字典生成错误的 URL

    当从查询字符串中提取的模型将字典作为其属性之一时 Swagger 会生成不正确的 URL 如何告诉 Swagger 更改 URL 中字典的格式或手动定义输入参数模式而不自动生成 尝试使用 Swashbuckle 和 NSwag 控制器 pu
  • 如何创建向后兼容 Windows 7 的缩放和尺寸更改每显示器 DPI 感知应用程序?

    我是 WPF 和 DPI 感知 API 的新手 正在编写一个在 Windows 7 8 1 和 10 中运行的应用程序 我使用具有不同每个显示器 DPI 设置的多个显示器 并且有兴趣将我的应用程序制作为跨桌面配置尽可能兼容 我已经知道可以将

随机推荐

  • Laravel Backpack - 显示关系函数中的特定属性

    我已经注册了Comment模型有一个User参考 像这样 public function user return this gt belongsTo App User 该函数返回一个实例User 这是正确的 但我不知道如何注册User列获取
  • jquery 捕获单词值

    有没有办法用 jquery 或 javascript 捕获单词值 在示例中 搜索 五月行情 当我单击 搜索 或 引号 或任何单词时 我会提醒该单词文本吗 Update 这就是我的意思 http jsfiddle net BE68L http
  • 动态代码生成

    我目前正在开发一个应用程序 您可以用它创建 程序 而无需编写源代码 如果您愿意 只需单击并播放即可 现在的问题是如何从我的数据模型生成可执行程序 有很多种可能性 但我不确定哪一种最适合我 我需要生成包含类和命名空间以及可以成为应用程序一部分
  • 谷歌应用程序脚本桌面IDE [重复]

    这个问题在这里已经有答案了 我喜欢将 Google Sheets 与应用程序脚本一起使用 但在线脚本 IDE 很麻烦 滞后 等 并且没有桌面 IDE 的许多便利 希望谷歌能在某个时候推出桌面IDE 有人知道制作 Google 应用程序脚本的
  • 传单中的javascript地图如何刷新

    我通过使用传单 API 在 javascript 中有一个基本的 geoJson 程序 div style width 100 height 400px div
  • 在 Laravel 8 中捕获 HTTP 客户端错误

    你如何捕捉抛出的错误HTTP客户端 https laravel com docs 8 x http client 例如超时 以便在您可以对错误执行任何操作以避免停止执行之前 它不会在 Laraval 调试器 在调试模式下 中抛出curl 错
  • 表视图中的文本标签太长,会影响正确的详细信息(detailTextLabel)被覆盖或不显示

    我已经为该单元格设置了一个文本 但是 它显示的文本太长 这会影响正确的详细文本被覆盖或不显示 我无法更改它 因为我需要下一个视图控制器中的名称 是否可以使其仅显示文本 后跟 EXAMPLE 电气电子工程 01 gt 传奇 Electrica
  • 如何在插件架构中做到免注册COM

    我们使用清单文件来实现免注册 COM 正如我在这另一个问题 https stackoverflow com questions 465882 generate manifest files for registration free com
  • 在哪里添加 String 原型

    我目前正在 Titanium Studio 中使用 JavaScript CommonJS 并且有一个关于原型设计的问题 假设我想向现有的类添加一个新函数 例如 String prototype trim function return t
  • 运行应用程序中的 Grails 3.0 静态 html

    之前曾就 grails 2 3 4 提出过类似的问题 我觉得很奇怪 我找不到一种方法来做到这一点 因为这对我来说似乎是一个标准用例 我只是想在运行 grails run app 时提供 html 页面 包括它们链接的 css 和 js An
  • 使用 asyncio 并行化生成器

    我的应用程序从慢速 I O 源读取数据 进行一些处理 然后将其写入本地文件 我已经用生成器实现了这个 如下所示 import time def io task x print requesting data for input s x ti
  • 您能否返回适用于任何可能的错误类型的结果?

    我想使用多个库 每个库都有自己的错误类型 我并不真正关心每个特定板条箱的错误类型 我想使用 习惯用法是使用那些返回 a 的 crate 的方法Result type 我也不想解开这些值 如果遇到错误 这会导致恐慌 我可能只是想使用传播不同的
  • 光滑的旋转木马 - 强制幻灯片具有相同的高度

    我在使用 Slick carousel JS 插件时遇到了多个问题幻灯片显示它们具有不同的高度 我需要幻灯片相同高度 但是对于 CSS flex box 它不起作用 因为幻灯片具有冲突的 CSS 定义 另外 我在论坛和网络上没有找到任何有用
  • 单例类与具有静态成员的类

    尽管关于该主题的主题有很多 但我仍然不清楚何时选择哪种方法 我希望通过讨论一个具体的例子 我最终能 明白 注意 我这里的语言是 Cocoa 尽管一般问题不是特定于语言的 我有一个类 TaskQueue 我想用它来 从我的代码中的任何位置访问
  • 在 Java 8 中使用两个具有相同签名的默认方法实现两个接口

    假设我有两个接口 public interface I1 default String getGreeting return Good Morning public interface I2 default String getGreeti
  • 如何将 jPCT 与 Vuforia SDK 结合使用?

    有人可以概述一下混合 Jpct 和 Qualcomm Vuforia SDK 的情况吗 将 Java 渲染引擎与 Vuforia 结合使用是个好主意吗 基本上 QCAR 的全部工作就是计算变形的预定义目标图像的变换矩阵 它为您提供了两个重要
  • 新的 Angular2 路由器配置

    回到使用已弃用的路由器时 我能够执行 router config 并传入一个对象 事实是 路由器本身在应用程序启动后进行了一些配置 该对象具有相同的 模板 就像我使用了 RouterConfig 一样 我正在寻找的是是否有一种方法可以像这样
  • POST 403 Forbidden for Chrome 扩展(后端为 Django)

    我以前从未开发过 Chrome 扩展程序 目前正在为我的 Django 应用程序开发 Chrome 扩展程序 具有链接提交功能 当我尝试使用扩展程序提交链接时 出现以下错误 POST http 127 0 0 1 8000 add link
  • 找到未使用的 Javascript 函数?

    我的应用程序中有大约 100 个 js 文件 我需要从这些文件中找到未使用的函数 哪个编辑器或工具可以帮助我 看看JSLint http jslint com help html代码质量工具
  • 如何在 Windows Phone 中对 LongListSelector 进行排序

    我希望能够按升序或降序对绑定到 LongListSelector 的数据进行排序 我无法将排序后的数据绑定到我的 LongListSelector 最初 我的解决方案没有尝试实现排序 而是有效的 但我相信在涉及排序时我遗漏了一些东西 我也尝