Repository navigation
Reference
Complete reference documentation for Myra UI Generator.
The generator recognizes the following Myra XML elements and maps them to C# types:
| XML Element | Generated C# Type | Notes |
|---|---|---|
Label |
Label |
Standard text label |
Button |
Button |
Button widget |
CheckBox |
CheckBox |
Checkbox control |
TextBox |
TextBox |
Single-line text input |
Panel |
Panel |
Container panel |
VerticalStackPanel |
VerticalStackPanel |
Vertical layout container |
HorizontalStackPanel |
HorizontalStackPanel |
Horizontal layout container |
Grid |
Grid |
Grid layout container |
ScrollViewer |
ScrollViewer |
Scrollable container |
Slider |
Slider |
Numeric slider control |
ProgressBar |
ProgressBar |
Progress indicator |
ListBox |
ListBox |
List selection control |
ComboBox |
ComboBox |
Dropdown selection |
Image |
Image |
Image display widget |
TextBlock |
TextBlock |
Rich text display |
| Unknown | Widget |
Fallback for unrecognized types |
If the generator encounters an XML element it doesn't recognize, it will:
- Use
Widgetas the property type (the base type for all Myra widgets) - Still generate the property if the element has an
Idattribute - You can cast to the specific type at runtime if needed
Example:
// Generated code for unknown widget type
public Widget CustomWidget { get; private set; }
// Usage with casting
var custom = _ui.CustomWidget as MyCustomWidgetType;Generated files follow this pattern:
-
XML File:
TitleScreen.xml -
Generated File:
TitleScreenUI.g.cs -
Class Name:
TitleScreenUI
The .g.cs suffix indicates a generated file (standard .NET convention).
Each generated class is a partial class with:
-
Properties: One property per widget with an
Idattribute - Initialize Method: Binds widgets from the root widget
Example generated code:
using Myra.Graphics2D.UI;
using System;
namespace YourNamespace.UI.Generated;
/// <summary>
/// Auto-generated UI accessor for TitleScreen.xml
/// This file is automatically generated by MyraUIGenerator - do not edit manually.
/// </summary>
public partial class TitleScreenUI
{
public Button StartButton { get; private set; }
public Label TitleLabel { get; private set; }
public Button SettingsButton { get; private set; }
/// <summary>
/// Initialize widgets from the loaded UI root.
/// Call this after loading the XML via UiLoader.Load().
/// </summary>
public void Initialize(Widget root)
{
StartButton = root.FindChildById("StartButton") as Button;
TitleLabel = root.FindChildById("TitleLabel") as Label;
SettingsButton = root.FindChildById("SettingsButton") as Button;
}
}-
Property Name: Matches the
Idattribute exactly (case-sensitive) - Property Type: Based on XML element name (see Supported Widgets table)
-
Access Modifier:
public -
Mutability:
private set(read-only from outside, set byInitialize)
Generated classes are partial, allowing you to extend them:
// Your code: MainMenuUI.cs
public partial class MainMenuUI
{
public void Show() { /* custom logic */ }
public void Hide() { /* custom logic */ }
}All generated properties follow this pattern:
public {WidgetType} {PropertyName} { get; private set; }-
WidgetType: The Myra widget type (e.g.,
Button,Label) -
PropertyName: Matches the XML
Idattribute - Access: Public getter, private setter
-
Initialization: Set to
nulluntilInitialize()is called
public void Initialize(Widget root)Parameters:
-
root(Widget): The root widget fromUiLoader.Load().Root
Behavior:
- Finds each widget by its
IdusingFindChildById() - Casts to the appropriate type
- Sets the corresponding property
- Properties may be
nullif widget not found or type mismatch
Usage:
var project = UiLoader.Load("TitleScreen.xml");
var ui = new TitleScreenUI();
ui.Initialize(project.Root);Important Notes:
- Must be called after loading the XML
- Must be called before accessing properties
- Properties will be
nullif widgets aren't found - No error is thrown if a widget is missing (check for null)
All generated types inherit from Myra.Graphics2D.UI.Widget:
Widget (base)
├── Label
├── Button
├── CheckBox
├── TextBox
├── Panel
│ ├── VerticalStackPanel
│ ├── HorizontalStackPanel
│ └── Grid
├── ScrollViewer
├── Slider
├── ProgressBar
├── ListBox
├── ComboBox
├── Image
└── TextBlock
The generator provides compile-time type safety:
// ✅ Correct - IntelliSense and type checking
ui.StartButton.Click += OnClick;
ui.TitleLabel.Text = "Hello";
// ❌ Compile error - wrong type
ui.StartButton.Text = "Hello"; // Button doesn't have Text property
// ❌ Compile error - property doesn't exist
ui.NonExistentButton.Click += OnClick;The generator reports diagnostic messages during compilation. These help debug configuration and generation issues.
Severity: Warning
Message: Error processing {file}: {error}
Description: An error occurred while processing a specific XML file.
Common Causes:
- Invalid XML syntax
- Malformed XML structure
- File encoding issues
- File access permissions
Resolution:
- Validate XML syntax
- Check file encoding (should be UTF-8)
- Ensure file is readable
- Check XML structure matches Myra format
Severity: Warning
Message: MyraUIGenerator executing. Namespace: {namespace}, Directory: {directory}, AdditionalFiles count: {count}
Description: Informational message showing generator is running with current configuration.
Use: Verify configuration is being read correctly.
Severity: Warning
Message: Found {count} XML files matching directory '{directory}'
Description: Shows how many XML files were found matching the configured directory.
Use: Verify XML files are being discovered.
If count is 0:
- Check
AdditionalFilesin.csproj - Verify directory path in configuration
- Check file paths match directory setting
Severity: Warning
Message: Generated {className}UI with {count} widgets
Description: Success message indicating a UI class was generated.
Use: Confirm generation succeeded.
Severity: Warning
Message: No widgets with Id found in {fileName}.xml
Description: XML file was processed but no widgets with Id attributes were found.
Resolution:
- Add
Idattributes to widgets you want to access - Verify XML structure is correct
- Check that widgets are nested properly
Severity: Error
Message: MyraUIGenerator threw an exception: {exception}
Description: An unexpected exception occurred in the generator itself.
Resolution:
- Check the exception details in the message
- Verify project configuration
- Ensure all dependencies are installed
- Report as a bug if issue persists
Visual Studio:
- Check Error List window
- Build Output window (set to "Detailed" or "Diagnostic")
Command Line:
dotnet build --verbosity detailedVS Code:
- Check Problems panel
- Terminal output during build
- See Examples for usage patterns
- Read Troubleshooting for common issues
- Check Advanced Topics for advanced usage