Opening the camera from .NET MAUI is one thing. Keeping it open for retakes, then getting the right photo back when someone taps Close? That’s the part I wanted to test.

Here’s what worked for ID Capture Shooter: I bound the Kotlin camera library on Android and the Swift camera framework on iOS, then put both behind one C# interface. The MAUI view model asks for JPEG bytes. If you close without taking a photo, it gets null and leaves the previous photo alone. It doesn’t need to know which camera did the work.

I’ll walk through the refactored MVVM screen, how I bound each native library, and what I checked on two real phones. The ID Capture Shooter sample repo includes the C# app and both compiled bindings, so you don’t need the native source to run it.

Four steps: open the camera, line up the test card, take a photo, then tap Close to see the photo back in .NET MAUI. Close without a shot to keep the previous photo.

Figure 1. Frame the card, take a shot, then tap Close to see the photo back in .NET MAUI.

When do you need a native library binding?

If you just need a photo, start with MAUI’s media picker. I used bindings because I wanted to call camera screens that I built and could run independently on each platform. Kotlin owns the CameraX screen; Swift owns the UIKit screen backed by AVFoundation. The ID frame is a guide for alignment, not a crop or a document scanner. This is a proof of concept for test subjects, not real IDs.

Define one C# interface for both platforms

The page has one job here: show the photo. I didn’t want it checking which phone you’re on or figuring out what the camera sent back. So I gave the view model one thing to call, ICardCapture. It gets JPEG bytes if you took a photo, or null if you closed the camera without taking one.

C# · Shared interface

public interface ICardCapture { // Null means that the user dismissed the native screen without taking a photo. Task<byte[]?> CaptureAsync(); } 

The platform choice happens in MauiProgram.cs: I register that platform’s NativeCardCapture as ICardCapture. By the time CaptureViewModel gets it in the constructor, there’s nothing left to check. The camera button runs an AsyncRelayCommand from CommunityToolkit.Mvvm, and CanOpenCamera keeps a second tap from opening another session.

C# · Camera command in CaptureViewModel.cs

public CaptureViewModel(ICardCapture camera) { this.camera = camera; OpenCameraCommand = new AsyncRelayCommand(CaptureAsync, CanOpenCamera); } public IAsyncRelayCommand OpenCameraCommand { get; } 

Say you already have a photo, reopen the camera, and change your mind. I don’t want Close to wipe out the photo you were looking at. So the view model only replaces Photo when new bytes come back. A failure is different from changing your mind: the view model shows an error and clears the busy state so you can try again.

C# · Capture and preview state in CaptureViewModel.cs

private async Task CaptureAsync() { if (IsCapturing) return; SetCapturing(true); SetError(null); try { var captured = await camera.CaptureAsync(); if (captured is not null && SetProperty(ref photo, captured, nameof(Photo))) { OnPropertyChanged(nameof(HasPhoto)); OnPropertyChanged(nameof(CameraActionText)); } } catch (Exception) { SetError("Couldn't return the photo. Try opening the camera again."); } finally { SetCapturing(false); } } 

This is where keeping the camera out of the page pays off. The button just binds to the command, and the text on it comes from CameraActionText. The MAUI Community Toolkit’s ByteArrayToImageSourceConverter turns those bytes into something the view can display. The page doesn’t need a camera click handler, and the view model doesn’t need to know how MAUI displays an image.

C# · Button bindings in CapturePage.cs

button.SetBinding(Button.CommandProperty, nameof(CaptureViewModel.OpenCameraCommand)); button.SetBinding(Button.TextProperty, nameof(CaptureViewModel.CameraActionText)); 

Bind the Kotlin and Swift libraries

Getting C# to call the camera is only half the trip. That’s the binding’s job. The platform adapter opens the camera, handles what comes back when you close it, and gives the view model JPEG bytes if there’s a photo. The .NET MAUI native library interop guide walks through the general setup. Here’s what that split looked like in my app.

The shared .NET MAUI screen opens the native camera: Kotlin on Android or Swift on iOS. Framing, shooting, and retakes happen in the native screen. Tap Close to return to MAUI and see the photo.

Figure 2. Kotlin or Swift runs the camera. .NET MAUI opens it and shows the photo when you return.

Android: Bind the Kotlin AAR

On Android, I started in kotlin/ and ran ./gradlew :idcapture:assembleRelease. That gave me idcapture-release.aar, which I added to the binding project with <AndroidLibrary>. Here’s the detail I’d check before moving on: the AAR doesn’t carry CameraX or AndroidX with it. The binding project brings those in separately.

The Kotlin entry point exposes static methods for creating the activity intent and reading the returned content URI:

Kotlin · IdCapture.kt excerpt

@JvmStatic fun createIntent(context: Context): Intent = Intent(context, CaptureActivity::class.java) @JvmStatic fun readPhotoUri(data: Intent?): Uri? = data?.data?.takeIf { it.scheme == "content" } 

I didn’t need the whole Kotlin library exposed to C#. In Metadata.xml, I named the generated type CaptureIntentFactory and left the internal Kotlin types out. MAUI uses CaptureIntentFactory.CreateIntent(this) to launch the camera through Android’s activity-result API. The part to watch in MainActivity.cs is what comes back: a content URI after a shot, or no photo if you cancel.

C# · Android activity result callback

public void OnActivityResult(Java.Lang.Object? result) { var activityResult = result as ActivityResult; var photo = activityResult?.ResultCode == (int)Result.Ok ? CaptureIntentFactory.ReadPhotoUri(activityResult.Data) : null; NativeCardCapture.Complete(photo); } 

That URI is Android’s problem, not the shared page’s. The adapter opens it through ContentResolver, copies the JPEG bytes into memory, and deletes the temporary image. By the time MAUI shows the photo, there’s no Android URI left for the page to deal with.

iOS: Bind the Swift XCFramework

Same goal on iOS: give MAUI one way to open the camera without exposing everything behind it. From the project root, I ran bash swift/build-xcframework.sh to build IDCaptureKit.xcframework for devices and simulators. The iOS binding includes it through <NativeReference Kind="Framework">. On the Swift side, one Objective-C-compatible factory was enough:

Swift · IDCapture.swift excerpt

@MainActor @objc(makeCaptureViewControllerWithCompletion:) public static func makeCaptureViewController( completion: @escaping (URL?, NSError?) -> Void ) -> UIViewController { CaptureViewController(completion: completion) } 

In ApiDefinition.cs, I map the Objective-C class IDCapture and its selector makeCaptureViewControllerWithCompletion: to the C# factory. See that colon at the end? Keep it. It’s part of the selector because the method takes one argument: the callback.

C# · iOS binding definition excerpt

[BaseType(typeof(NSObject), Name = "IDCapture")] [DisableDefaultCtor] interface CaptureControllerFactory { [Static] [Export("makeCaptureViewControllerWithCompletion:")] UIViewController MakeCaptureViewController(CaptureCompletion completion); } 

On iOS, the MAUI app supplies the camera permission message in Info.plist. It opens the controller full-screen and dismisses it when the callback fires. This time, a successful capture comes back as a file URL instead of an Android content URI. The cleanup is the same: read the JPEG, delete the temporary file, and give the view model the bytes.

One thing caught me on iOS: the framework was embedded and the app built, but it didn’t launch. The loader was looking for the framework under /Library/Frameworks. Setting its install name to @rpath/IDCaptureKit.framework/IDCaptureKit when archiving the Swift framework fixed it. A green build doesn’t tell you the app can load the library.

Test photo capture and cancellation on real phones

I opened the camera from MAUI on an iPhone SE and a OnePlus Nord N30 5G. That’s the Swift screen on the left and the Kotlin screen on the right. Both have a rotating ID guide, a shutter that lets you try another shot, and a separate Close button.

Swift camera on iPhone and Kotlin camera on OnePlus, both opened from .NET MAUI

Figure 3. MAUI opens the native camera: Swift on iPhone SE, Kotlin on OnePlus Nord N30 5G.

After the MVVM refactor, I ran that same round trip again on both phones. I took a shot and tapped Close. The photo appeared in the shared MAUI preview. Then I reopened the camera and closed it without shooting. The previous photo stayed put on both phones, just as the view model intended.

The captured test photo displayed back in the MAUI app on iPhone and OnePlus

Figure 4. After Close, the shared MAUI page shows the JPEG returned by each library.

Before you bind a whole library, give yourself one finish line you can see. Mine was a photo showing up on the same MAUI page on both phones. Once that round trip works, you’ve got something concrete to test every time you add to the binding.

Finally...

Thanks for building this with me. The lesson I’m taking into the next binding: keep the native code behind a small C# interface, and test what happens on real phones when you take a photo and when you close without one. When the Great Xperience newsletter opens, subscribe for more hands-on code posts, debugging stories, and the occasional bug that insists it’s a feature. See you in the next one!

I also published this walkthrough on Great Xperience Academy.