【问题标题】:ICollectionView could not be found from Studio Code on .NET Core在 .NET Core 上的 Studio Code 中找不到 ICollectionView
【发布时间】:2020-04-27 08:08:09
【问题描述】:

我有一个XAML 和一个MVVM 显示一个DataGrid 和一个ObservableCollection。 一切正常(我之前的更多细节,我自己已经回答了,question)。

现在我正在尝试添加一个过滤器,我想关注@mark-heath tutorial

我的项目构建引发以下问题

error CS0246: The type or namespace name 'ICollectionView' could not be found (are you missing a using directive or an assembly reference?) 

即使我确实包含了documented namespace

using System.ComponentModel;

关于哪个 Studio Code 改为显示 Unnecessary using directive。 我的 .csproj 在 .NET Core 3.0 上

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>

问题似乎与我添加程序集引用的方式有关。

dotnet add package WindowsBase

它使用 .NET Framework 恢复包,但可能这是错误的,因为我使用的是 .NET Core 和 Linux

哪种方法是正确的?除了“你不能那样做”的答案...也许有另一个,等效的包要添加...来自 Avalonia UI?有人知道或使用它吗?

在 Avalonia UI github 上搜索并在 gitter 上询问

我看到有一个已经关闭的github issue,所以现在可能有解决方案吗? (我也在询问 Avalonia UI gitter channel

【问题讨论】:

  • 您是否考虑过使用DynamicData 代替CollectionViewSource?这是一种便携的解决方案,也更加灵活。

标签: c# .net-core avaloniaui


【解决方案1】:

首先 - 即使在 WPF 和 Windows 上 - 根据我的comment 那里的教程是错误的:

您需要绑定到 ICollectionView 而不是 ObservableCollection查看过滤效果。

所以,视图必须固定如下

<DataGrid Items="{Binding PeopleView}" 

现在,回到 Linux 和 Avalonia UI 中的问题。

正如Steven Kirk 所建议的那样,我在 avalonia 中查看了 github DevTools source,它在视图模型中进行过滤,而这条线索可以解决问题。

所以我在视图模型中声明PeopleView

public DataGridCollectionView PeopleView { get; }

作为 DataGridCollectionView 包含在所需的命名空间中

using Avalonia.Collections;

我终于可以实现过滤器了

public MainWindowViewModel()
{
    People = new ObservableCollection<Person>(GenerateMockPeopleTable());
    PeopleView = new DataGridCollectionView(People);
    PeopleView.Filter = o => String.IsNullOrEmpty(Filter) ? true : ((Person)o).FirstName.Contains(Filter); 

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-04
    • 2016-11-15
    • 1970-01-01
    • 2022-11-29
    • 2020-07-20
    • 2020-07-24
    • 2021-06-05
    • 2020-01-15
    相关资源
    最近更新 更多