First decide whether you need a reusable composition or a new behavior contract. In declarative Rust, a component packages a view; in XAML, a UserControl does. A templated control exposes properties and lets consumers replace its visuals through a template.
Start with a reusable composition
Section titled “Start with a reusable composition”A labeled settings group usually needs composition, not new input behavior. As a component, its parameters are its properties:
use ferroui_view::prelude::*;
#[component]pub fn NotificationSettings(email: Signal<bool>, desktop: Signal<bool>) -> Element { rsx! { StackPanel { spacing: 8.0, TextBlock { text: "Notifications", font_size: 20.0 } CheckBox { is_checked: email.two_way(), "Send email updates" } CheckBox { is_checked: desktop.two_way(), "Show desktop notifications" } } }}A component is used like a control (NotificationSettings { email, desktop } in rsx!, or notification_settings().email(..) with its builder), and can be shown inside XAML through a ComponentHost (components with XAML).
In XAML, this view fragment can become the content of a UserControl:
<UserControl xmlns="https://github.com/ferroui"> <StackPanel Spacing="8"> <TextBlock Text="Notifications" FontSize="20" /> <CheckBox Content="Send email updates" /> <CheckBox Content="Show desktop notifications" /> </StackPanel></UserControl>Move the view into your application’s normal XAML registration/build flow. Bind the options to model properties once the layout works. Keep its inherited data context usable by consumers instead of unconditionally replacing it inside the control.
If consumers need to restyle the entire visual structure, define a templated control instead. Write down its properties, events, default values, keyboard behavior, and required template parts before adding rendering code. The HamburgerMenu sample shows a complete application control declaration.
Define the public contract
Section titled “Define the public contract”Choose the base class, content model, styled/direct properties, events, and required template parts. Register markup metadata if consumers should construct the control from XAML. Provide defaults that work before the first property assignment.
A Rust method alone does not establish a styled property. Use the framework’s property registration and class declaration patterns so binding, inheritance, styling, and diagnostics can all see the value.
Respect the lifecycle
Section titled “Respect the lifecycle”Release template-part subscriptions when a template is replaced. Avoid retaining a detached visual tree through a closure. A control can attach, detach, or receive a new template more than once during its lifetime.
Call the relevant base implementation when overriding framework behavior unless the contract explicitly requires replacement. Keep UI-thread restrictions consistent with the rest of the control system.
Test behavior and presentation
Section titled “Test behavior and presentation”Test default properties, notification, input behavior, retemplating, resource changes, and disposal. Use headless tests for deterministic control behavior and rendering tests for visuals that matter to the contract.
Use samples/SampleControls and existing framework controls as implementation references, then consult the generated API for the exact traits and registration signatures of your documented version.
Public APIs and controls
Section titled “Public APIs and controls”component— reusable declarative composition.- UserControl — reusable XAML view composition.
TemplatedControl— a replaceable visual template.StyledProperty— a property that participates in styling and binding.- CheckBox — reuse its option and keyboard behavior.
- XAML registration, themes, and testing.