Xamarin新手求教:如何开发适配iOS与Android的单一类库?
Absolutely! This is one of the core advantages of using Xamarin—you can absolutely consolidate your shared business logic into a single library while still handling platform-specific functionality (like launching ViewControllers or Activities) where needed. Let’s walk through how to make this work for your setup:
You’ve got two solid options for building a shared logic library in Xamarin:
- .NET Standard Class Library: This is the recommended approach. It compiles into a single binary that works seamlessly across Xamarin.iOS, Xamarin.Android, and other .NET platforms. Your cross-platform business logic lives here, and you can connect to platform-specific code via abstractions (more on that below).
- Shared Project: This is a file-level sharing method—your shared code files are included directly in each platform project during compilation. It’s flexible for quick conditional compilation (using
#if IOSor#if ANDROIDdirectives) but doesn’t produce a standalone library. For most maintainability needs, .NET Standard is the cleaner choice.
The key is to separate your universal business logic from platform-specific UI/navigation code:
- Move all shared logic (like data processing, API calls, business rules) into your new shared library. This is the code that doesn’t care if it’s running on iOS or Android.
- Abstract platform-specific functionality: For actions like launching a ViewController or Activity, define an interface in your shared library. For example:
public interface INavigationService { void LaunchFeaturePage(); } - Implement the interface in your platform libraries: Keep your existing iOS.dll and Android.dll (or update them) to handle the platform-specific work. In the iOS implementation, you’d instantiate and present the ViewController; in Android, you’d start the target Activity.
To use the platform-specific implementation from your shared logic, inject the INavigationService instance into your shared classes. This keeps your shared library decoupled from platform details.
For example, in your shared business logic:
public class FeatureManager { private readonly INavigationService _navigationService; public FeatureManager(INavigationService navigationService) { _navigationService = navigationService; } public void StartFeature() { // Run shared business logic first ProcessFeatureData(); // Trigger platform-specific navigation _navigationService.LaunchFeaturePage(); } private void ProcessFeatureData() { // Cross-platform data processing logic here } }
Then, in your Xamarin.iOS app, register the iOS implementation:
// In your AppDelegate or startup code var navigationService = new iOSNavigationService(); var featureManager = new FeatureManager(navigationService);
And similarly for Xamarin.Android:
// In your MainActivity or startup code var navigationService = new AndroidNavigationService(); var featureManager = new FeatureManager(navigationService);
You have two paths here:
- Keep your existing iOS.dll and Android.dll as platform-specific implementation projects (just add a reference to your new shared library in them), or
- Move the platform-specific code directly into your Xamarin.iOS/Xamarin.Android app projects if that fits your workflow better. Either way, the shared library remains the single source of truth for your core business logic.
This approach cuts down on code duplication, makes updating your business logic across platforms easier, and keeps your setup organized.
内容的提问来源于stack exchange,提问作者Sahil Khanna

