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 : PageRepresents a page that displays a single view. This is the most common type of page used in Xamarin.Forms applications.
ListView
public class ListView : ViewA control that displays a scrollable list of items. It can be data-bound to collections and supports virtualization for performance.
Key properties include:
ItemsSource: The collection of items to display.ItemTemplate: ADataTemplatedefining how each item in the list is displayed.
Button
public class Button : ViewRepresents a button control that triggers an action when clicked.
Common events:
Clicked: Fired when the button is tapped.
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
Android.App: Core application components like Activities and Services.Android.Content: Mechanisms for application interaction and data transfer.Android.Views: UI elements and layout management.Android.Widget: Common UI controls.
Activity Class
public class Activity : ContextThemeWrapperRepresents a single screen with a user interface. Activities are the building blocks of an Android application.
Key methods:
OnCreate(Bundle savedInstanceState): Called when the activity is first created.OnStart(): Called when the activity becomes visible to the user.OnResume(): Called when the activity will interact with the user.
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
UIKit: For building user interfaces with controls like Views, Buttons, and Navigation Controllers.Foundation: Core non-UI types and services.CoreGraphics: For 2D drawing and graphics operations.
UIViewController Class
public class UIViewController : UIResponderManages a view hierarchy and provides the event handling for your application's screens.
Key methods:
ViewDidLoad(): Called after the controller's view has been loaded into memory.ViewWillAppear(bool animated): Called just before the view is added to the view hierarchy.ViewDidAppear(bool animated): Called just after the view is added to the view hierarchy.
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 : ViewDisplays plain text to the user.
EditText
public class EditText : TextViewAllows the user to enter text.
Button
public class Button : TextViewA 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 : UIViewDisplays read-only text.
UITextField
public class UITextField : UIControlA single-line text entry control.
UIButton
public class UIButton : UIControlA 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 ConnectivityProvides access to network connectivity information.
Connectivity.NetworkAccess Property
public static NetworkAccess NetworkAccess { get; }Gets the current network access state.
Possible values:
NetworkAccess.InternetNetworkAccess.ConstrainedInternetNetworkAccess.LocalNetworkAccess.None
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 GeolocationProvides 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 PreferencesProvides 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 FilePickerAllows 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:
- Xamarin.Android:
Android.OS.Environmentfor external storage,Context.GetSharedPreferences()for internal preferences. - Xamarin.iOS:
NSFileManagerfor file system access,NSUserDefaultsfor preferences.
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 : classResolves 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.