Linux下Mono编译C#项目报错:找不到ListCollectionView
Alright, let's tackle this issue you're facing with compiling your C# project using Mono on Linux. The error about missing ListCollectionView stems from common gaps in WPF-related assembly handling in Mono, and here are the actionable fixes to get your project compiling:
1. Explicitly reference required WPF assemblies
ListCollectionView lives in the PresentationFramework.dll assembly, which isn't included by default in standard Mono compilations. You need to explicitly reference this (and supporting) assemblies when compiling.
When using mcs (Mono's C# compiler), run your compile command like this:
mcs -r:PresentationFramework.dll -r:WindowsBase.dll -r:System.dll SNAPoolLib.cs
PresentationFramework.dll: ContainsListCollectionViewand other WPF data binding typesWindowsBase.dll: Required forCollectionViewSourceand core WPF infrastructureSystem.dll: Covers basic system types (safe to specify even if auto-included by default)
2. Install Mono's WPF components
If you still get assembly-not-found errors after adding references, you likely don't have the Mono WPF packages installed on your Linux system.
For Debian/Ubuntu-based systems:
sudo apt-get install mono-devel mono-wpf
For RPM-based systems (like Fedora):
sudo dnf install mono-devel mono-wpf
This ensures all WPF-related libraries are present on your machine.
3. Switch to a more portable interface (alternative approach)
If you don't rely on ListCollectionView's specific features, consider using the ICollectionView interface instead. This makes your code more cross-platform compatible (works with .NET Framework, .NET Core, and Mono) and avoids tying to a concrete WPF implementation.
Modify your code like this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using System.Windows.Data; namespace SNAPoolLib { public class ObservableList<T>: ObservableCollection<T> { ICollectionView _p_view; public ICollectionView _View { get { if (_p_view == null) { // No casting needed - GetDefaultView returns ICollectionView directly _p_view = CollectionViewSource.GetDefaultView(this); } return _p_view; } } public ObservableList() : base() { } public ObservableList(IEnumerable<T> items) : base(items) { } } }
This change preserves all core collection view functionality (filtering, sorting, etc.) while improving portability.
4. Update to a recent Mono version
Older Mono releases had incomplete WPF support. If you're running an outdated version, upgrade to the latest stable release (6.x or newer) to ensure better compatibility with WPF types.
内容的提问来源于stack exchange,提问作者user5728072

