Xamarin API Documentation

Welcome to the official API documentation for Xamarin. This resource provides detailed information about the classes, methods, and properties available across the Xamarin platform. Leverage this documentation to build powerful, cross-platform native applications with C# and .NET.

Xamarin allows you to share code across Android, iOS, and macOS projects. This documentation covers the APIs for both Xamarin.Forms (for cross-platform UI) and platform-specific libraries like Xamarin.Android and Xamarin.iOS.

Xamarin.Forms

Xamarin.Forms is a cross-platform UI toolkit that allows you to build native UIs for Android, iOS, and macOS from a single, shared C# codebase. It abstracts platform-specific UI elements into a common API.

Core Classes

ContentPage

public class ContentPage : Page

Represents a page that displays a single view. This is the most common type of page used in Xamarin.Forms applications.

ListView

public class ListView : View

A control that displays a scrollable list of items. It can be data-bound to collections and supports virtualization for performance.

Key properties include:

Button

public class Button : View

Represents a button control that triggers an action when clicked.

Common events:

Common Properties:

Text: The text displayed on the button.

Command: An ICommand to be executed when the button is clicked.

CommandParameter: Parameter to pass to the Command.

Xamarin.Android

Xamarin.Android allows you to build native Android applications using C#. It provides access to the entire Android SDK.

Key Namespaces

Activity Class

public class Activity : ContextThemeWrapper

Represents a single screen with a user interface. Activities are the building blocks of an Android application.

Key methods:

Note: You must declare each Activity in your application's AndroidManifest.xml file.

Xamarin.iOS

Xamarin.iOS allows you to build native iOS applications using C#. It provides access to the entire iOS SDK, including UIKit, Foundation, and more.

Key Namespaces

UIViewController Class

public class UIViewController : UIResponder

Manages a view hierarchy and provides the event handling for your application's screens.

Key methods:

Warning: UI updates must be performed on the main thread using InvokeOnMainThread or similar mechanisms.

Android UI Controls

Xamarin.Android provides access to a rich set of native Android UI controls.

TextView

public class TextView : View

Displays plain text to the user.

EditText

public class EditText : TextView

Allows the user to enter text.

Button

public class Button : TextView

A standard push-button.

Note: Layouts are typically defined using XML resource files in the Resources/layout directory.

iOS UI Controls

Xamarin.iOS offers comprehensive access to Apple's UIKit framework.

UILabel

public class UILabel : UIView

Displays read-only text.

UITextField

public class UITextField : UIControl

A single-line text entry control.

UIButton

public class UIButton : UIControl

A standard button control.

Note: You can design UIs using Storyboards or programmatically using C# code.

Connectivity

The Xamarin.Essentials library provides an API to check the network connectivity status of the device.

Connectivity Class

public static class Connectivity

Provides access to network connectivity information.

Connectivity.NetworkAccess Property

public static NetworkAccess NetworkAccess { get; }

Gets the current network access state.

Possible values:

Connectivity.ConnectivityChanged Event

public static event EventHandler ConnectivityChanged;

Fired when the network connectivity changes.

Geolocation

Xamarin.Essentials provides a cross-platform API for accessing the device's location.

Geolocation Class

public static class Geolocation

Provides methods to retrieve the device's current location.

Geolocation.GetLocationAsync() Method

public static Task<Location> GetLocationAsync(GeolocationRequest request)

Retrieves the device's current location asynchronously.

Parameters:

request: A GeolocationRequest object specifying desired accuracy and timeout.

Returns: A Task<Location> representing the asynchronous operation. The result is a Location object containing latitude, longitude, altitude, and other details.

Warning: Ensure you have requested location permissions from the user.

Preferences

Xamarin.Essentials offers a simple way to store and retrieve key-value pairs of primitive data types.

Preferences Class

public static class Preferences

Provides methods for storing and retrieving application preferences.

Preferences.Set() Method

public static void SetValue<T>(string key, T value)

Saves a value associated with a unique key.

Preferences.Get() Method

public static T GetValue<T>(string key, T defaultValue = default(T))

Retrieves a value associated with a key, with an optional default value.

Note: Supported types include bool, double, float, int, long, string, DateTime, and TimeSpan.

Storage

Accessing device storage can be done using platform-specific APIs or cross-platform solutions.

Xamarin.Essentials.FilePicker

public static class FilePicker

Allows users to pick files from their device.

FilePicker.PickAsync() Method

public static Task<FileResult> PickAsync(PickOptions options)

Presents the native file picker to the user.

Platform-Specific Storage

For more advanced storage needs, refer to the platform-specific documentation:

Custom Renderers

Custom Renderers in Xamarin.Forms allow you to create highly customized native controls by extending the platform-specific renderer classes.

IVisualElementRenderer Interface

The core interface implemented by all renderers.

RendererBase<TElement> (Abstract Class)

A base class provided by Xamarin.Forms that simplifies the creation of custom renderers.

Registering a Custom Renderer

Use the [assembly: ExportRenderer(...)] attribute in your platform-specific projects.

[assembly: ExportRenderer(typeof(MyCustomViewCell), typeof(MyCustomViewCellRenderer))]

Note: Custom renderers are essential for implementing platform-specific behaviors or appearances not covered by standard Xamarin.Forms controls.

DependencyService

The DependencyService is a service locator that allows Xamarin.Forms applications to call native platform-specific code.

DependencyService.Register<T>()

Registers an implementation of an interface for dependency injection.

DependencyService.Get<T>()

public static T Get<T>() where T : class

Resolves and returns an instance of the registered implementation for the specified interface.

// In your shared code
            public interface IPlatformService { void DoSomethingNative(); }

            // In your Xamarin.Android project
            [assembly: Xamarin.Forms.Internals.Preserve(AllMembers = true)]
            [assembly: Dependency(typeof(AndroidPlatformService))]
            public class AndroidPlatformService : IPlatformService { ... }

            // In your Xamarin.iOS project
            [assembly: Xamarin.Forms.Internals.Preserve(AllMembers = true)]
            [assembly: Dependency(typeof(iOSPlatformService))]
            public class iOSPlatformService : IPlatformService { ... }

            // Usage
            var platformService = DependencyService.Get<IPlatformService>();
            platformService.DoSomethingNative();

Note: The [assembly: Preserve(AllMembers = true)] attribute is often required to prevent the linker from removing necessary types.

MessagingCenter

MessagingCenter is a publish-subscribe system that allows loosely coupled communication between different parts of your application.

MessagingCenter.Subscribe()

Subscribes to a message with a specific sender and action.

MessagingCenter.Publish()

Publishes a message to all subscribers.

MessagingCenter.Unsubscribe()

Unsubscribes from a message.

// Publisher
            MessagingCenter.Send<MySenderType, string>(this, "MyMessage", "Hello from sender!");

            // Subscriber
            MessagingCenter.Subscribe<MySenderType, string>(this, "MyMessage", (sender, arg) =>
            {
                System.Diagnostics.Debug.WriteLine($"Received: {arg}");
            });

            // Unsubscribe when no longer needed
            MessagingCenter.Unsubscribe<MySenderType, string>(this, "MyMessage");

Note: Always unsubscribe from messages when the subscriber is no longer active to prevent memory leaks.