Xamarin.Forms引用PCL引发Android平台System.IO.FileNotFoundException求助
Hey there, let's work through this annoying Android-only issue you're hitting. First, let's break down why this is happening:
This error pops up because of compatibility gaps between old PCL profiles and .NET Standard libraries, specifically when you reference a .NET Standard 2.0 class library in your Xamarin.Forms PCL project targeting .NET Framework 4.5. Mono for Android's runtime profile doesn't include the System.Configuration assembly by default, whereas iOS's runtime handles this reference differently—hence the platform-specific failure.
Here are the most effective fixes, ordered by how robust they are:
1. Upgrade to .NET Standard 2.0 (Recommended)
The cleanest long-term fix is to align your projects on .NET Standard 2.0:
- Either recompile the referenced PCL library to target .NET Standard 2.0, or upgrade your Xamarin.Forms PCL project to .NET Standard 2.0.
- To do this: Right-click the project → Properties → Application → Target framework, then select
.NET Standard 2.0. - This eliminates the compatibility mismatch entirely, as .NET Standard is designed for cross-platform consistency. You might need to check for minor API differences, but .NET Standard 2.0 has excellent overlap with PCL profile 4.5.
2. Add the Official Configuration NuGet Package
If upgrading frameworks isn't feasible right now, install the official NuGet package to fill the gap:
- In your Xamarin.Android project, install
System.Configuration.ConfigurationManagervia NuGet. This package provides theSystem.Configurationfunctionality for .NET Standard and Xamarin environments. - Once installed, the runtime will correctly resolve the assembly without throwing the FileNotFoundException.
3. Manually Reference the Assembly (Temporary Workaround)
If you prefer not to use NuGet, you can manually add the reference to your Android project:
- Right-click your Xamarin.Android project → Add → Reference.
- Go to the Assemblies tab, search for
System.Configuration, and check the box to include it. - Alternatively, edit your Android project's
.csprojfile to add this snippet (adjust the hint path to match your package version):<ItemGroup> <Reference Include="System.Configuration"> <HintPath>..\packages\System.Configuration.ConfigurationManager.4.7.0\lib\netstandard2.0\System.Configuration.ConfigurationManager.dll</HintPath> </Reference> </ItemGroup>
4. Audit Hidden Dependencies
Sometimes System.Configuration is being pulled in indirectly by a third-party package in your referenced PCL:
- Expand the Dependencies node in your project to trace which package is adding this reference.
- Try replacing that package with a version that targets .NET Standard 2.0, or remove any unnecessary dependencies that aren't critical to your app.
内容的提问来源于stack exchange,提问作者Jorge MHT

