能否使用.NET 6开发移动应用并保留平台专属UI实现(不采用MAUI跨平台UI方案)?
Absolutely! You can absolutely build mobile apps with .NET 6 while keeping fully platform-specific UI implementations (UIViewControllers on iOS, Activities on Android) without relying on MAUI's cross-platform UI stack. Here's a breakdown of how to make this work, along with practical examples:
This approach leverages the official .NET platform-specific mobile SDKs (formerly Xamarin Native) that are fully integrated with .NET 6. They let you write 100% native UI code in C# while sharing business logic, data access, or other cross-cutting code via shared .NET libraries.
Step 1: Set Up Your Solution Structure
Start by creating a multi-project solution with three components:
- A .NET Class Library (net6.0) for shared code: This is where you'll put logic that doesn't depend on platform-specific APIs (e.g., data models, API clients, business rules).
- An iOS App (net6.0-ios) project: This will contain all your UIViewControllers, storyboards/XIBs, and iOS-specific logic.
- An Android App (net6.0-android) project: This will hold your Activities, XML layouts, and Android-specific code.
Step 2: Implement Platform-Specific UI
For iOS (Using UIKit & UIViewControllers)
You can build UI either visually via Xcode's Interface Builder (storyboards/XIBs) or programmatically with C#—just like native iOS development, but with C# syntax.
Example of a programmatic UIViewController:
using UIKit; using CoreGraphics; namespace MyiOSApp { public class HomeViewController : UIViewController { public override void ViewDidLoad() { base.ViewDidLoad(); View.BackgroundColor = UIColor.SystemBackground; // Add a welcome label programmatically var welcomeLabel = new UILabel { Text = "Hello from .NET 6 + UIKit!", Frame = new CGRect(20, 120, View.Bounds.Width - 40, 32), TextAlignment = UITextAlignment.Center, Font = UIFont.SystemFontOfSize(20, UIFontWeight.Semibold) }; View.AddSubview(welcomeLabel); } } }
For Android (Using Activities & XML Layouts)
Similarly, you'll create Activities and XML layouts exactly as you would in native Android development, but with C# for the logic layer.
Example of an Activity with a linked XML layout:
using Android.App; using Android.OS; using Android.Widget; namespace MyAndroidApp { [Activity(Label = "@string/app_name", MainLauncher = true)] public class MainActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_main); // Update text from the layout var welcomeText = FindViewById<TextView>(Resource.Id.welcome_text); welcomeText.Text = "Hello from .NET 6 + Android Activities!"; } } }
Corresponding Resources/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/welcome_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textStyle="bold"/> </LinearLayout>
Step 3: Share Logic Across Platforms
Reference your shared .NET 6 class library from both the iOS and Android projects, then call its methods directly from your platform-specific UI code.
Example shared service:
// In MySharedLibrary project namespace MySharedLibrary { public class AppDataService { public string GetSharedGreeting() { return "This message comes from shared .NET 6 code!"; } } }
Use it in iOS:
var dataService = new MySharedLibrary.AppDataService(); welcomeLabel.Text = dataService.GetSharedGreeting();
Use it in Android:
var dataService = new MySharedLibrary.AppDataService(); welcomeText.Text = dataService.GetSharedGreeting();
Key Things to Know
- Tooling Support: You can use Visual Studio 2022 (Windows or Mac) to build these projects—full support for Xcode's Interface Builder on Mac and Android's Layout Editor on both platforms.
- No MAUI Dependencies: You won't need to add any MAUI NuGet packages or use MAUI's cross-platform controls. Your UI is 100% native, just written in C#.
- Platform APIs: You have full access to all native iOS/Android APIs via C# bindings, so you can implement any platform-specific feature you need (e.g., Core Location on iOS, LocationManager on Android).
内容的提问来源于stack exchange,提问作者MobileDev

