Reference
Build a Xui app in C#, from your first page to responsive desktop and mobile interfaces, custom controls, Canvas drawing, and GPU rendering.
Follow these guides to build an app by hand, then explore the features your product needs. Articles include compiled examples, runnable demos where the renderer supports them, and explicit platform limits. Each page also has a Markdown version with the same explanations and expanded source code for coding assistants.
Start with Getting Started, then follow App foundations to build a small app. Then follow the topics your app needs. The API reference provides the current public declarations alongside these guides.
Contents
App foundations
Your first app
- Getting Started - Install .NET and the Xui template, create an app, and run it in the emulator.
- Understand the generated project - Shared app sources, platform entry points, project files, assets, and the role of each package.
- Application, window, and view - How an application creates a window, how a root view hosts content, and where platform runtimes fit.
- Build your first page - Compose a logo, text, and buttons; respond to a click; update what the user sees.
- Run and iterate - Choose a host, use the phone emulator, resize and rotate, and understand when to rebuild or restart.
Layout and adaptive interfaces
- How layout works - Measure and arrange, constraints, desired size, alignment, margin, and padding.
- Stacks and FlexBox - Build rows and columns, distribute space, align children, and wrap content.
- Grid layouts - Use fixed, automatic, and fractional tracks, spans, and named areas.
- Adapt to available space - Choose width thresholds, reflow content, and share a page between a phone and a resizable desktop window.
- Safe areas and device geometry - Account for display scale, orientation, insets, and the on-screen keyboard.
- Scrolling content - Size a scroll view, nest it in layouts, and handle content larger than the viewport.
Controls and visual design
- Core views and ToolKit widgets - Choose between basic views and styled buttons, cards, toggles, checkboxes, and other shared widgets.
- Themes and design tokens - Apply consistent colors, typography, spacing, and corner shapes; respond to preference changes.
- Text, fonts, and images - Load assets, choose fonts, measure text, and size images without distortion.
- Lists and repeated content - Present collections, handle selection, and understand the current list implementation's limits for large data sets.
- Accessible interaction - Design readable text, visible focus, useful target sizes, and keyboard alternatives; document current native accessibility support and gaps.
State and application services
- State and UI updates - Keep state in the right place and request layout or rendering when it changes.
- Services and dependency injection - Register and resolve shared services with Xui.Core.DI and give windows and pages the dependencies they need.
- Async work and the UI thread - Load data without blocking, dispatch UI changes, show progress and errors, and cancel work when a page closes.
- Application and window lifetime - Start and quit, respond to window changes, and distinguish desktop lifetime from mobile platform lifecycle behavior.
- Own and release resources - Dispose images and GPU resources, unsubscribe handlers, and avoid retaining pages after navigation.
Navigation, views, and input
Pages and navigation
- Create a page - Structure a page with content, actions, and local state using ToolKit navigation primitives.
- Connect pages with NavigationShell - Define navigation items, select a destination, and choose the initial page.
- Mobile navigation and back - Move from a home page into child pages, return to the previous page, and account for platform back behavior.
- Desktop navigation - Use a left menu to change the main content, with compact and expanded navigation at narrow widths.
- Page state and lifetime - Pass data to a destination, decide when to preserve or recreate content, and stop page work when navigating away.
- Overlays and additional windows - Choose an in-window popup or a separate desktop window; open, dismiss, and clean it up with its owner.
Custom views
- Create a View subclass - Implement a minimal custom view and understand its layout, rendering, and input responsibilities.
- Measure and arrange a custom view - Respect constraints, report sizes, place children, and avoid layout feedback loops.
- Render and invalidate - Draw a view, request another frame when needed, and keep drawing separate from state changes.
- Attach, detach, and reuse - Manage view lifetime, inherited state, subscriptions, and resources as the view enters or leaves the tree.
- Build a reusable interactive control - Combine layout, drawing, pointer input, keyboard access, focus, and disabled states in one control.
- Transforms, clipping, and layers - Position and transform content, clip drawing and input appropriately, and work with retained layers.
Events and pointer input
- Follow an event through Xui - Trace runtime events through the window, root view, hit testing, and view event routing; understand propagation and handling.
- Mouse interaction on desktop - Handle movement, buttons, hover, and leaving a view without assuming touch has a hover state.
- Touch interaction on mobile - Track contact identities, phases, multiple contacts, and cancellation.
- Shared pointer gestures - Build tap and drag behavior with the pointer gesture contracts, capture, and competing gesture candidates.
- Scrolling and trackpads - Handle wheel deltas and touch scrolling, nested scroll regions, and platform-specific momentum information.
- Zoom and coordinate conversion - Handle magnification separately from scrolling, map input into local coordinates, and interact with transformed content.
Keyboard and text input
- Focus and keyboard navigation - Move focus, use Tab and Shift+Tab, show focus feedback, and restore focus after an overlay closes.
- Keys and shortcuts - Handle key presses and modifiers, distinguish commands from text entry, and account for platform conventions.
- TextBox and TextInput - Add editable fields, choose input options, bind app state, and show validation feedback.
- Editing and selection - Understand caret movement, selection, deletion, clipboard operations, and Unicode text boundaries.
- IME and soft keyboards - Distinguish committed text from composition, request an appropriate mobile keyboard, and keep the focused field visible; identify runtime support gaps.
- Custom text editors - Implement the text-editing client contract and connect editing state, selection, rendering, and platform input.
Graphics and 3D
Canvas and 2D drawing
- Canvas fundamentals - Obtain an IContext, use Xui coordinate types, and save and restore drawing state.
- Shapes, paths, and curves - Draw rectangles, ellipses, lines, arcs, and Bezier paths with fills and strokes.
- Colors, gradients, and compositing - Apply paint, opacity, blend operations, and clipping with explicit backend support notes.
- Transforms and coordinate spaces - Translate, scale, and rotate; combine transforms and convert between world and view coordinates.
- Draw and measure text - Set fonts, use baselines and text metrics, align labels, and handle available width.
- Images and SVG output - Load and draw image assets, manage their lifetime, and capture vector output through the software SVG context.
- Build an interactive drawing surface - Combine drawing, hit testing, pan, zoom, and selection in a small diagram or chart.
Animation
- Frames and time - Use frame callbacks and elapsed time rather than assuming a fixed refresh rate.
- Easing and transitions - Animate values, interrupt or reverse a transition, and settle into a final state.
- Animate layout and drawing - Choose between changing layout, transforming a view, and animating Canvas content.
- Animation lifetime and motion preferences - Stop work for detached views, respect reduced motion where available, and avoid continuous rendering for idle content.
GPU and 3D drawing
This part of Xui is experimental. These articles state the supported backends and limitations of each example, including what happens when GPU rendering is unavailable.
- Choose Canvas or GPU rendering - Understand the two rendering paths, device capabilities, resource ownership, and fallback choices.
- Your first GPU triangle - Create buffers, a pipeline, and a render target, then submit and display a frame.
- Write shaders in C# - Define vertex and fragment stages, pass data between them, and understand compile-time shader translation and its restrictions.
- Meshes, textures, and uniforms - Describe vertex data, upload geometry and textures, and update shader parameters.
- 3D coordinates and cameras - Work with vectors and matrices, model/view/projection transforms, perspective, and viewport sizing.
- Depth, culling, and lighting - Render overlapping surfaces and a lit mesh, with explicit support requirements for the example.
- Build an interactive 3D view - Display a rotating model, add mouse and touch camera controls, resize safely, and release resources when leaving the page.
Testing and release
Testing
- Test app logic and controls - Choose unit, component, integration, or end-to-end tests for the behavior being checked.
- Deterministic UI tests - Use the test runtime to control time and input, navigate pages, and make assertions without a native window.
- Visual regression tests - Capture SVG or raster output, use consistent fonts and device geometry, and review snapshot changes.
Debugging and performance
- Inspect a running app - Use DevTools and the MCP tooling to inspect views, capture screenshots, and reproduce input sequences.
- Debug layout and input - Diagnose constraints, clipping, hit testing, focus, gesture conflicts, and desktop/mobile differences.
- Measure performance and allocations - Find unnecessary layouts and frames, inspect resource use, and keep hot input and drawing paths allocation-conscious.
Running and shipping
- Desktop apps - Build and run on Windows and macOS, configure native windows, and understand distribution prerequisites.
- Mobile apps - Set up Android and iOS tooling, run on simulators and devices, and distinguish emulator behavior from native behavior.
- Browser apps - Build WebAssembly assets, host them statically, and account for loading, browser restrictions, and deployment paths.
- Platform support and fallbacks - Check the actual runtime implementation, design around unavailable features, and keep platform-specific code at the edge of the app.
- Troubleshooting setup and builds - Diagnose SDK, workload, package, native toolchain, and asset-loading problems with reproducible commands.