### Getting Started Guide
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/MANIFEST.txt
A guide for new users to set up and begin using the DialogHost.
```APIDOC
## getting-started.md
### Description
Quick start guide for new users, covering setup, basic patterns, and initial usage.
### Steps
1. Overview (README.md)
2. Setup and Patterns (this document)
3. XAML Details (xaml-reference.md)
4. Troubleshooting (INDEX.md)
```
--------------------------------
### Minimal DialogHost Setup
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Shows a basic DialogHost setup with simple TextBlock elements for both dialog content and main content.
```xml
```
--------------------------------
### Dialog with Custom Theme Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/styling-reference.md
Example of a DialogHost using custom background and overlay brushes, with a thin accent border and rounded corners. Requires defining `CustomOverlayBrush`, `CustomBackgroundBrush`, and `AccentBrush` resources.
```xml
```
--------------------------------
### Minimalist Dialog Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/styling-reference.md
Example of a minimalist DialogHost with a semi-transparent overlay and no box shadow. Close on click away is enabled.
```xml
```
--------------------------------
### Initialize ViewModel on Dialog Open
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of initializing a ViewModel when a dialog is opened.
```csharp
await DialogHost.Show(content,
openedEventHandler: (s, args) => {
if (args.Session.Content is MyViewModel vm) {
vm.Initialize();
}
}
);
```
--------------------------------
### Modern Card-Style Dialog Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/styling-reference.md
Example of a DialogHost styled as a modern card with rounded corners, no border, and a subtle shadow. Close on click away is enabled.
```xml
```
--------------------------------
### Install DialogHost.Avalonia NuGet Package
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/README.md
Use this command to add the DialogHost.Avalonia package to your project.
```shell
dotnet add package DialogHost.Avalonia
```
--------------------------------
### Legacy Setup for DialogHostStyles
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/styling-reference.md
Shows the legacy method for setting up DialogHostStyles using StyleInclude, applicable for versions prior to 0.7.
```xml
```
--------------------------------
### Simple Confirmation Dialog Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Demonstrates creating a reusable confirmation dialog with Yes/No buttons. The ViewModel initiates the dialog and handles the result.
```csharp
public class ConfirmationViewModel {
public async Task ShowConfirmation() {
var dialog = new ConfirmationDialog();
var result = await DialogHost.Show(dialog, "MainDialogHost");
if (result is true) {
// Proceed
}
}
}
public class ConfirmationDialog : UserControl {
public ConfirmationDialog() {
this.InitializeComponent();
}
}
```
```xml
// XAML for ConfirmationDialog
```
--------------------------------
### Dynamic Content Update Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialogsession-class.md
Shows how to update dialog content dynamically after an initial display, typically after a delay or user interaction.
```csharp
await DialogHost.Show(step1Content, (sender, args) => {
// Wait for user interaction, then update
await Task.Delay(1000);
args.Session.UpdateContent(step2Content);
});
```
--------------------------------
### Setup DialogHostStyles in Application
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/styling-reference.md
Demonstrates how to add the DialogHostStyles to your application's style collection in XAML. This is required for the dialog system to function.
```xml
```
--------------------------------
### Styled DialogHost Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Shows how to apply custom styling to DialogHost, including properties for background, margin, blur effects, and custom attached properties for visual appearance.
```xml
```
--------------------------------
### Programmatic Assignment of Dialog Popup Positioners
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/popup-positioners.md
Examples of assigning different popup positioners programmatically to the dialogHost.
```csharp
// Center dialog
dialogHost.PopupPositioner = CenteredDialogPopupPositioner.Instance;
// Align to top-right with margins
dialogHost.PopupPositioner = new AlignmentDialogPopupPositioner {
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10, 10, 10, 0)
};
// Custom positioner
dialogHost.PopupPositioner = new CustomPositioner();
```
--------------------------------
### Dialog with Blur Background Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/styling-reference.md
Example of a DialogHost with a blurred background effect, significant corner radius, and a pronounced box shadow. The `BlurBackgroundRadius` property controls the blur intensity.
```xml
```
--------------------------------
### Access DialogSession in DialogOpenedEventArgs
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example demonstrating how to access the DialogSession from DialogOpenedEventArgs to interact with the opened dialog's content and state.
```csharp
(object sender, DialogOpenedEventArgs args) => {
var session = args.Session;
Console.WriteLine($"Content: {session.Content}");
// Update content after 1 second
Task.Delay(1000).ContinueWith(_ => {
session.UpdateContent(newContent);
});
}
```
--------------------------------
### DialogHost Base Class Hierarchy
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/types-enums.md
Shows the inheritance hierarchy for the DialogHost class, starting from System.Object.
```text
System.Object
└── Avalonia.AvaloniaObject
└── Avalonia.Controls.Control
└── Avalonia.Controls.ContentControl
└── DialogHostAvalonia.DialogHost
```
--------------------------------
### Coordinate Dialog Open and Close Events
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example demonstrating coordination between opened and closing event handlers, including updating content and handling cancellation.
```csharp
await DialogHost.Show(content,
openedEventHandler: (s, args) => {
Console.WriteLine("Dialog opened");
args.Session.UpdateContent(enrichedContent);
},
closingEventHandler: (s, args) => {
Console.WriteLine($"Dialog closing with result: {args.Parameter}");
if (args.CanBeCancelled && args.Parameter == null) {
args.Cancel();
}
}
);
```
--------------------------------
### Handle Dialog Opened Event
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of subscribing to the DialogOpened event to perform actions when a dialog is opened, such as updating its content.
```csharp
DialogOpenedEventHandler handler = (sender, args) => {
Console.WriteLine($"Dialog opened on {sender}");
var session = args.Session;
session.UpdateContent(modifiedContent);
};
await DialogHost.Show(content, handler);
```
--------------------------------
### Popup Positioners Reference
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/MANIFEST.txt
Reference for the IDialogPopupPositioner interface and its implementations, including centered and alignment positioners, and examples for custom positioners.
```APIDOC
## Popup Positioners
### Interfaces
- **IDialogPopupPositioner**: Interface for defining dialog popup positioning logic.
- **IDialogPopupPositionerConstrainable**: Interface for positioners that can be constrained.
### Classes
- **CenteredDialogPopupPositioner**: Implements positioning to center the dialog.
- **AlignmentDialogPopupPositioner**: Implements positioning based on alignment.
### Examples
- **Custom positioner examples**: Three examples demonstrating how to implement custom positioners.
```
--------------------------------
### Alignment Dialog Popup Positioner in XAML
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/popup-positioners.md
Example of configuring an AlignmentDialogPopupPositioner directly in XAML using local namespace.
```xml
```
--------------------------------
### Ensuring DialogHost in Visual Tree
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Provides an example of correct XAML structure to ensure the DialogHost is part of the visual tree before attempting to show a dialog.
```xml
```
--------------------------------
### Lazy Initialization of DialogHost
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/advanced-topics.md
Uses lazy initialization to create and configure a DialogHost instance only when it's first accessed, deferring setup until needed.
```csharp
public class LazyDialogHost {
private Lazy _dialogHost;
public LazyDialogHost() {
_dialogHost = new Lazy(() => {
var host = new DialogHost { Identifier = "LazyHost" };
Application.Current?.MainWindow?.Content = host;
return host;
});
}
public async Task Show(object content) {
return await DialogHost.Show(content, "LazyHost");
}
}
```
--------------------------------
### Animated Dialog Positioner
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/advanced-topics.md
Create an animated dialog positioner by implementing IDialogPopupPositioner. This example animates the dialog from above the center to the center position over a specified duration.
```csharp
public class AnimatedPositioner : IDialogPopupPositioner {
private double _animationProgress = 0.0;
private DateTime _startTime;
private const double AnimationDuration = 300; // milliseconds
public Rect Update(Size anchorRectangle, Size size) {
if (_startTime == DateTime.MinValue) {
_startTime = DateTime.Now;
}
var elapsed = (DateTime.Now - _startTime).TotalMilliseconds;
_animationProgress = Math.Min(1.0, elapsed / AnimationDuration);
// Easing function: ease-out cubic
var eased = 1.0 - Math.Pow(1.0 - _animationProgress, 3.0);
var centerX = (anchorRectangle.Width - size.Width) / 2;
var centerY = (anchorRectangle.Height - size.Height) / 2;
// Start from above, animate down to center
var startY = centerY - 50;
var currentY = startY + (centerY - startY) * eased;
return new Rect(centerX, currentY, size.Width, size.Height);
}
}
```
--------------------------------
### Custom Constrained Dialog Positioner
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/advanced-topics.md
Implement both IDialogPopupPositioner and IDialogPopupPositionerConstrainable for positioners that constrain available space. This example applies a margin to the dialog area.
```csharp
public class ConstrainedPositioner : IDialogPopupPositioner, IDialogPopupPositionerConstrainable {
public Thickness Margin { get; set; } = new Thickness(40);
public Rect Update(Size anchorRectangle, Size size) {
var margin = Margin;
var constrainedWidth = Math.Max(0, size.Width - margin.Left - margin.Right);
var constrainedHeight = Math.Max(0, size.Height - margin.Top - margin.Bottom);
var x = margin.Left;
var y = margin.Top;
return new Rect(x, y, constrainedWidth, constrainedHeight);
}
public Size Constrain(Size availableSize) {
return new Size(
Math.Max(0, availableSize.Width - Margin.Left - Margin.Right),
Math.Max(0, availableSize.Height - Margin.Top - Margin.Bottom)
);
}
}
```
--------------------------------
### Multi-Step Wizard Implementation
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Demonstrates creating a multi-step wizard where each step can request to advance to the next content. The `openedEventHandler` is used to set up the navigation logic.
```csharp
var step1 = new Step1View();
var result = await DialogHost.Show(
content: step1,
openedEventHandler: (s, args) => {
var step1Vm = (Step1ViewModel)args.Session.Content;
step1Vm.NextRequested += async (newContent) => {
args.Session.UpdateContent(newContent);
};
}
);
```
--------------------------------
### DialogHost with Binding and Commands
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Illustrates how to configure DialogHost using data binding for properties like IsOpen and DialogContent, and how to utilize its OpenDialogCommand and CloseDialogCommand.
```xml
```
--------------------------------
### Session Internal Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/overlays-internal.md
Gets the DialogSession for this popup, initialized in the constructor. Its lifetime is the same as the popup.
```csharp
internal DialogSession Session { get; }
```
--------------------------------
### Show() - With DialogHost Instance
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog directly on a specified DialogHost instance, with optional callbacks for opening and closing events.
```APIDOC
## Show() - With DialogHost Instance
### Description
Shows a modal dialog on a specific `DialogHost` instance (direct reference instead of identifier).
### Method
`public static Task Show(object? content, DialogHost instance)
public static Task Show(
object? content,
DialogHost instance,
DialogOpenedEventHandler? openedEventHandler,
DialogClosingEventHandler? closingEventHandler)
`
### Parameters
#### Path Parameters
- `content` (object?) - Optional - Content to display
- `instance` (DialogHost) - Required - Target `DialogHost` instance
- `openedEventHandler` (DialogOpenedEventHandler?) - Optional - Callback when dialog opens
- `closingEventHandler` (DialogClosingEventHandler?) - Optional - Callback before dialog closes
### Response
#### Success Response (Task)
- Returns `Task` - result from the close parameter
#### Throws
- `ArgumentNullException` - if `instance` is `null`
### Request Example
```csharp
var result = await DialogHost.Show(content, dialogHostInstance);
```
```
--------------------------------
### PopupPositioner Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/overlays-internal.md
Gets or sets the custom positioner for this popup. This property is bound from DialogHost.PopupPositioner.
```csharp
public IDialogPopupPositioner? PopupPositioner { get; set; }
```
--------------------------------
### Static API Usage
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Demonstrates how to open, close, and query the state of dialogs using the static DialogHost methods.
```APIDOC
## Static API
### Description
Provides static methods for direct interaction with the DialogHost to manage dialogs.
### Methods
- **DialogHost.Show(content, identifier)**: Opens a dialog.
- **DialogHost.Close(identifier, parameter)**: Closes a dialog.
- **DialogHost.IsDialogOpen(identifier)**: Checks if a dialog is currently open.
### Request Example
```csharp
// Open dialog
var result = await DialogHost.Show(content, "identifier");
// Close dialog
DialogHost.Close("identifier", parameter);
// Query state
var isOpen = DialogHost.IsDialogOpen("identifier");
```
### See Also
[dialoghost-class.md](dialoghost-class.md)
```
--------------------------------
### Dispose Content on Dialog Close
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of disposing of content that implements IDisposable when a dialog is closing.
```csharp
await DialogHost.Show(content,
closingEventHandler: (s, args) => {
if (args.Session.Content is IDisposable disposable) {
disposable.Dispose();
}
}
);
```
--------------------------------
### Validation on Dialog Close
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of preventing dialog closure if validation fails, by cancelling the close operation.
```csharp
await DialogHost.Show(formContent,
closingEventHandler: (s, args) => {
if (args.Parameter is FormData data && !IsValid(data)) {
// Validation failed - prevent close
if (args.CanBeCancelled) {
args.Cancel();
}
}
}
);
```
--------------------------------
### Access DialogSession in DialogClosingEventArgs
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example showing how to retrieve the DialogSession from DialogClosingEventArgs to inspect the content of the dialog that is about to close.
```csharp
(object sender, DialogClosingEventArgs args) => {
var session = args.Session;
Console.WriteLine($"Closing dialog with content: {session.Content}");
}
```
--------------------------------
### CurrentSessions Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets a list of all currently open dialog sessions. Empty list if no dialogs are open.
```APIDOC
## CurrentSessions Property
### Description
Gets a list of all currently open dialog sessions. Empty list if no dialogs are open.
### Type
`IReadOnlyList`
### Read-only
Yes
```
--------------------------------
### Show() Internal Method
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/overlays-internal.md
Adds the popup to the visual tree and sets focus. It ensures the popup is a child of DialogHost.Root and then calls Focus(). Called by the IsOpen property setter when set to true.
```csharp
internal void Show()
```
--------------------------------
### DialogOpenedCallback Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets a callback invoked when the dialog opens. Alternative to subscribing to the DialogOpened event.
```APIDOC
## DialogOpenedCallback Property
### Description
Gets or sets a callback invoked when the dialog opens. Alternative to subscribing to the `DialogOpened` event.
### Type
`DialogOpenedEventHandler?`
### Default
`null`
### Binding Mode
Two-way
### Dependency Property
`DialogOpenedCallbackProperty`
```
--------------------------------
### Show() Static Method
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog with the provided content. Returns a task that completes when the dialog closes. The command parameter is passed to close handlers and returned from the Show() method.
```csharp
var result = await DialogHost.Show(new MyDialogViewModel());
if (result is bool confirmed && confirmed) {
// Dialog was closed with true
}
```
--------------------------------
### CurrentSessions Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets a list of all currently open dialog sessions. Empty list if no dialogs are open.
```csharp
public IReadOnlyList CurrentSessions { get; }
```
--------------------------------
### DialogOpenedCallback Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets a callback invoked when the dialog opens. Alternative to subscribing to the DialogOpened event.
```csharp
public DialogOpenedEventHandler? DialogOpenedCallback { get; set; }
```
--------------------------------
### Display Dialog Using Direct Instance Reference
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Show a dialog by providing a direct reference to the DialogHost instance.
```csharp
// If you have a reference to the DialogHost
var result = await DialogHost.Show(
content: dialogContent,
instance: myDialogHostInstance
);
```
--------------------------------
### DialogHost for Multiple Dialogs
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Shows how to configure DialogHost to manage multiple dialogs simultaneously and trigger their opening.
```xml
```
--------------------------------
### DisableOpeningAnimation Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/overlays-internal.md
Gets or sets whether the opening animation is disabled for this popup. This property is bound from DialogHost.DisableOpeningAnimation.
```csharp
public bool DisableOpeningAnimation { get; set; }
```
--------------------------------
### Enable and Open Multiple Dialogs
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/advanced-topics.md
Enable the multiple dialogs mode on the DialogHost and then open multiple dialogs sequentially. The tasks for each dialog can be awaited independently.
```csharp
// Enable multiple dialogs mode
dialogHost.IsMultipleDialogsEnabled = true;
// Open first dialog
var task1 = DialogHost.Show(content1, "DialogHost");
// Open second dialog while first is still open
var task2 = DialogHost.Show(content2, "DialogHost");
// Wait for all dialogs
var result1 = await task1;
var result2 = await task2;
```
--------------------------------
### Binding DialogContent Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Example of dynamically setting dialog content by binding the DialogContent property to a ViewModel property.
```xml
```
--------------------------------
### Open Dialog Programmatically with DialogHost.Show
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/README.md
Utilize the static DialogHost.Show method for an async/await based approach to display dialogs directly from code, such as in a view model. Content can be passed as an argument. For multiple DialogHosts, use the Identifier property.
```csharp
DialogHost.Show(viewOrModel);
```
--------------------------------
### Show() - With Both Handlers and Identifier
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog with options for content, a specific DialogHost identifier, and both opening and closing event handlers.
```APIDOC
## Show() - With Both Handlers and Identifier
### Description
Shows a modal dialog with all options: custom content, identifier targeting, and event handlers.
### Method
`public static Task Show(
object? content,
string? dialogIdentifier,
DialogOpenedEventHandler? openedEventHandler,
DialogClosingEventHandler? closingEventHandler)
`
### Parameters
#### Path Parameters
- `content` (object?) - Optional - Content to display
- `dialogIdentifier` (string?) - Optional - Target `DialogHost` identifier
- `openedEventHandler` (DialogOpenedEventHandler?) - Optional - Callback when dialog opens
- `closingEventHandler` (DialogClosingEventHandler?) - Optional - Callback before dialog closes
### Response
#### Success Response (Task)
- Returns `Task` - result from the close parameter
### Request Example
```csharp
var result = await DialogHost.Show(
content: new ConfirmDialog(),
dialogIdentifier: "MainWindow",
openedEventHandler: (s, e) => Console.WriteLine("Opened"),
closingEventHandler: (s, e) => {
if (e.Parameter == null) e.Cancel();
}
);
```
```
--------------------------------
### BlurBackgroundRadius Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the radius of the background blur effect when BlurBackground is enabled. The default radius is 16.0.
```csharp
public double BlurBackgroundRadius { get; set; }
```
--------------------------------
### Cancel Dialog Closing Conditionally
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of cancelling a dialog close operation only if a certain condition is met and cancellation is allowed.
```csharp
(object sender, DialogClosingEventArgs args) => {
if (!userConfirmedAction && args.CanBeCancelled) {
args.Cancel();
}
}
```
--------------------------------
### DialogOverlayPopupHost Constructor
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/overlays-internal.md
Initializes a new DialogOverlayPopupHost, storing a reference to the parent DialogHost and creating a DialogSession with optional event handlers for dialog opening and closing.
```csharp
public DialogOverlayPopupHost(
DialogHost host,
DialogOpenedEventHandler? open,
DialogClosingEventHandler? closing)
{
// Initialization:
// - Stores parent DialogHost reference
// - Creates DialogSession with provided event handlers
// - Initializes DialogTaskCompletionSource for result handling
Host = host;
Session = new DialogSession(open, closing);
DialogTaskCompletionSource = new TaskCompletionSource();
}
```
--------------------------------
### DialogHost with Event Handlers
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Demonstrates how to attach event handlers for dialog opening and closing events directly in XAML.
```xml
```
--------------------------------
### Handle Dialog Closing with CanBeCancelled Check
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of handling the closing event and checking CanBeCancelled before attempting to cancel the operation.
```csharp
(object sender, DialogClosingEventArgs args) => {
if (args.CanBeCancelled) {
// Safe to call Cancel()
args.Cancel();
} else {
// Dialog is being forced closed, cannot cancel
Console.WriteLine("Dialog is being forcefully closed");
}
}
```
--------------------------------
### Async/Await Code-First Dialog Display
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Recommended pattern for displaying dialogs using async/await. Pass content directly to the DialogHost.Show method.
```csharp
var result = await DialogHost.Show(content);
if (result is bool confirmed && confirmed) {
// Handle confirmation
}
```
```csharp
var dialogContent = new TextBlock { Text = "Delete this file?" };
var result = await DialogHost.Show(
content: dialogContent,
dialogIdentifier: "MainDialogHost"
);
if (result is true) {
// User confirmed deletion
DeleteFile();
} else {
// User cancelled
}
```
--------------------------------
### Show Dialog with DialogHost Instance
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog directly on a provided DialogHost instance, bypassing the need for an identifier. Optionally accepts opening and closing event handlers. Throws ArgumentNullException if the instance is null.
```csharp
public static Task Show(object? content, DialogHost instance)
```
```csharp
public static Task Show(
object? content,
DialogHost instance,
DialogOpenedEventHandler? openedEventHandler,
DialogClosingEventHandler? closingEventHandler)
```
```csharp
var result = await DialogHost.Show(content, dialogHostInstance);
```
--------------------------------
### Event Arguments and Handlers
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/MANIFEST.txt
Documentation for event arguments and handlers related to dialog opening and closing, including methods for cancellation and event coordination patterns.
```APIDOC
## Event Arguments and Handlers
### Delegates
- **DialogOpenedEventHandler**: Delegate for handling the DialogOpened event.
- **DialogClosingEventHandler**: Delegate for handling the DialogClosing event.
### Event Argument Classes
- **DialogOpenedEventArgs**: Provides data for the DialogOpened event.
- **DialogClosingEventArgs**: Provides data for the DialogClosing event.
- **Cancel()**: Method within DialogClosingEventArgs to cancel the closing operation.
### Usage Patterns
- Event coordination patterns for managing dialog lifecycle events.
```
--------------------------------
### PopupTemplate Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the control template for the dialog popup container. Allows customization of the dialog visual structure.
```APIDOC
## PopupTemplate Property
### Description
Gets or sets the control template for the dialog popup container. Allows customization of the dialog visual structure.
### Type
`IControlTemplate?`
### Default
`null` (uses default template from styles)
### Dependency Property
`PopupTemplateProperty`
```
--------------------------------
### Show() - Static Method
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog with the provided content. Returns a task that completes when the dialog closes. The result is the value passed to the close parameter.
```APIDOC
## Show() - Static Method
### Description
Shows a modal dialog with the provided content. Returns a task that completes when the dialog closes.
### Method Signature
`public static Task Show(object? content)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Parameters
- **content** (`object?`) - Optional - Content to display (view model or control). If `null`, uses `DialogContent` from XAML.
### Returns
`Task` - result from the close parameter
### Throws
- `InvalidOperationException` - if no `DialogHost` instance is loaded in the visual tree
### Example
```csharp
var result = await DialogHost.Show(new MyDialogViewModel());
if (result is bool confirmed && confirmed) {
// Dialog was closed with true
}
```
```
--------------------------------
### PopupTemplate Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the control template for the dialog popup container. Allows customization of the dialog visual structure.
```csharp
public IControlTemplate? PopupTemplate { get; set; }
```
--------------------------------
### Customizing Dialog Host Positioning and Styling
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Demonstrates how to apply custom positioning logic and styling to the DialogHost. The PopupPositioner property can be set to a custom implementation, and styling properties like CornerRadius can be applied.
```csharp
// Positioning
dialogHost.PopupPositioner = new AlignmentDialogPopupPositioner { };
// Styling
DialogHostStyle.SetCornerRadius(host, new CornerRadius(8));
```
--------------------------------
### BlurBackground Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets whether to enable background blur when a dialog is opened. This property is used in conjunction with BlurBackgroundRadius.
```csharp
public bool BlurBackground { get; set; }
```
--------------------------------
### Show() - With Identifier
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog on a specific DialogHost instance identified by a string identifier. If no identifier is provided, it defaults to the first available instance.
```APIDOC
## Show() - With Identifier
### Description
Shows a modal dialog on a specific `DialogHost` instance (identified by `Identifier`).
### Method
`public static Task Show(object? content, string? dialogIdentifier)`
### Parameters
#### Path Parameters
- `content` (object?) - Optional - Content to display
- `dialogIdentifier` (string?) - Optional - Identifier of the target `DialogHost`. If `null`, uses the first available instance.
### Response
#### Success Response (Task)
- Returns `Task` - result from the close parameter
#### Throws
- `InvalidOperationException` - if no matching `DialogHost` instance is found or multiple unidentified instances exist
### Request Example
```csharp
var result = await DialogHost.Show(viewModel, "MainDialogHost");
```
```
--------------------------------
### Customization APIs Usage
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Illustrates how to customize dialog positioning and styling.
```APIDOC
## Customization APIs
### Description
Offers APIs for customizing the appearance and behavior of dialogs, including positioning and styling.
### Methods
- **Set PopupPositioner**: Assign a custom positioner to control dialog placement.
- **DialogHostStyle.SetCornerRadius**: Apply specific styling, like corner radius, to the dialog host.
### Request Example
```csharp
// Positioning
dialogHost.PopupPositioner = new AlignmentDialogPopupPositioner { };
// Styling
DialogHostStyle.SetCornerRadius(host, new CornerRadius(8));
```
### See Also
[popup-positioners.md](popup-positioners.md), [styling-reference.md](styling-reference.md)
```
--------------------------------
### OverlayBackground Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the background brush for the overlay, which is the dimmed area behind the dialog. The default is determined by the theme.
```csharp
public IBrush OverlayBackground { get; set; }
```
--------------------------------
### DialogContentTemplate Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the template used to display the dialog content, transforming a view model into a UI representation.
```csharp
public IDataTemplate? DialogContentTemplate { get; set; }
```
--------------------------------
### Enabling and Using Multiple Dialogs
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Shows how to enable the feature for displaying multiple dialogs simultaneously and how to open and close specific dialogs using their identifiers.
```csharp
// Enable in XAML
// Now you can open multiple dialogs simultaneously
var dialog1 = await DialogHost.Show(content1);
var dialog2 = await DialogHost.Show(content2); // Both open at same time
// Close specific dialog
DialogHost.Close("MainDialogHost", parameter: true, content: content1);
```
--------------------------------
### Show() - With Opened Handler
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog and invokes a provided callback function once the dialog has opened.
```APIDOC
## Show() - With Opened Handler
### Description
Shows a modal dialog and invokes a callback when the dialog opens.
### Method
`public static Task Show(object? content, DialogOpenedEventHandler openedEventHandler)`
### Parameters
#### Path Parameters
- `content` (object?) - Optional - Content to display
- `openedEventHandler` (DialogOpenedEventHandler) - Required - Callback invoked when dialog opens
### Response
#### Success Response (Task)
- Returns `Task` - result from the close parameter
### Request Example
```csharp
var result = await DialogHost.Show(content, (s, args) => {
Console.WriteLine("Dialog opened!");
args.Session.UpdateContent(newContent);
});
```
```
--------------------------------
### Open, Close, and Query Dialog State
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Demonstrates the basic static API for opening, closing, and checking the status of dialogs using identifiers.
```csharp
var result = await DialogHost.Show(content, "identifier");
DialogHost.Close("identifier", parameter);
var isOpen = DialogHost.IsDialogOpen("identifier");
```
--------------------------------
### XAML Declarative API Usage
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Shows how to declare a DialogHost in XAML and bind its `IsOpen` property.
```APIDOC
## XAML Declarative API
### Description
Enables the declaration of a DialogHost directly within XAML, allowing for declarative control over its visibility and content.
### XAML Example
```xml
```
### See Also
[xaml-reference.md](xaml-reference.md)
```
--------------------------------
### CloseDialogCommand Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets the command used to close the dialog. The command parameter is passed to close handlers and returned from the Show() method.
```APIDOC
## CloseDialogCommand Property
### Description
Gets the command used to close the dialog. The command parameter is passed to close handlers and returned from the `Show()` method.
### Type
`ICommand`
### Read-only
Yes
### Can Execute
When `IsOpen` is `true`
```
--------------------------------
### DialogHost Control (Main API)
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Documentation for the DialogHost control, including its properties, methods, routed events, and command properties.
```APIDOC
## DialogHost Control (Main API)
### Description
Provides documentation for the main DialogHost control, detailing its properties, methods, routed events, and command properties.
### Method
N/A (This is a reference to a class/control)
### Endpoint
N/A (This is a reference to a class/control)
### Parameters
#### Properties
- **30+ properties**: With types, defaults, and descriptions.
- **Command properties**: OpenDialogCommand, CloseDialogCommand.
#### Methods
- **15+ static and instance method overloads**
#### Events
- **2 routed events**: With event args.
### Request Example
N/A
### Response
N/A
### Remarks
- Constants and remarks are included.
```
--------------------------------
### CloseDialogCommand Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets the command used to close the dialog. The command parameter is passed to close handlers and returned from the Show() method.
```csharp
public ICommand CloseDialogCommand { get; }
```
--------------------------------
### Static DialogContent
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Demonstrates setting static, predefined content directly within the DialogHost's DialogContent property.
```xml
```
--------------------------------
### DialogMargin Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the margin around the dialog content within the dialog host bounds. The default margin is 0.
```csharp
public Thickness DialogMargin { get; set; }
```
--------------------------------
### Configure AlignmentDialogPopupPositioner
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/popup-positioners.md
Create an instance of AlignmentDialogPopupPositioner to control dialog placement using horizontal and vertical alignment, along with margins. This allows for flexible positioning similar to Avalonia's layout system.
```csharp
public class AlignmentDialogPopupPositioner : AvaloniaObject, IDialogPopupPositioner, IDialogPopupPositionerConstrainable
```
```csharp
public HorizontalAlignment HorizontalAlignment { get; set; }
```
```csharp
public VerticalAlignment VerticalAlignment { get; set; }
```
```csharp
public Thickness Margin { get; set; }
```
```csharp
var positioner = new AlignmentDialogPopupPositioner {
HorizontalAlignment = HorizontalAlignment.Right
};
```
```csharp
var positioner = new AlignmentDialogPopupPositioner {
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Bottom,
Margin = new Thickness(20) // 20 pixels from right and bottom edges
};
dialogHost.PopupPositioner = positioner;
```
--------------------------------
### Advanced Topics
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/MANIFEST.txt
In-depth documentation for advanced usage scenarios and customization.
```APIDOC
## advanced-topics.md
### Description
Covers advanced concepts and customization techniques for the DialogHost.
### Topics
- Custom dialog transitions
- Integration with complex MVVM scenarios
- Performance optimization
```
--------------------------------
### Basic DialogHost Declaration
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/xaml-reference.md
Demonstrates the minimal XAML required to declare a DialogHost element and specify its dialog content.
```xml
```
--------------------------------
### Access Close Parameter in DialogClosingEventArgs
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of how to access the parameter passed during a dialog close operation from DialogClosingEventArgs to determine the result or reason for closing.
```csharp
(object sender, DialogClosingEventArgs args) => {
if (args.Parameter is bool result) {
Console.WriteLine($"Dialog result: {result}");
} else {
Console.WriteLine("Dialog closed without a result");
}
}
```
--------------------------------
### Async/Await Task Coordination with DialogHost
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/advanced-topics.md
Coordinate sequential dialogs using async/await. Each dialog's result determines whether the next step in the sequence proceeds.
```csharp
public async Task ProcessDialogSequence() {
var step1Result = await DialogHost.Show(step1Content);
if (step1Result is true) {
var step2Result = await DialogHost.Show(step2Content);
if (step2Result is true) {
var confirmResult = await DialogHost.Show(confirmContent);
if (confirmResult is true) {
// All steps confirmed
await FinalizeOperation();
}
}
}
}
```
--------------------------------
### Get Dialog Session
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Retrieves the current dialog session for a given DialogHost identifier. This allows for programmatic control over the dialog, such as closing it with a result.
```csharp
public static DialogSession? GetDialogSession(string? dialogIdentifier)
```
```csharp
var session = DialogHost.GetDialogSession("MainDialogHost");
if (session != null) {
session.Close(result);
}
```
--------------------------------
### Command-Based Dialog Opening
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Open a dialog by binding a button's Command to the DialogHost's OpenDialogCommand. Use CommandParameter to pass content.
```xml
Confirmation Dialog
```
--------------------------------
### CloseOnClickAwayParameter Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the parameter passed to the close handler when the dialog is closed by clicking away. This parameter is accessible via DialogClosingEventArgs.Parameter.
```csharp
public object? CloseOnClickAwayParameter { get; set; }
```
--------------------------------
### CloseOnClickAway Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets whether clicking outside the dialog area (on the overlay) should close the dialog. This property supports two-way binding.
```csharp
public bool CloseOnClickAway { get; set; }
```
--------------------------------
### Show Dialog with All Options
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog with custom content, targeting a specific DialogHost by identifier, and utilizing both opening and closing event handlers. The closing handler can cancel the operation if a parameter is not provided.
```csharp
public static Task Show(
object? content,
string? dialogIdentifier,
DialogOpenedEventHandler? openedEventHandler,
DialogClosingEventHandler? closingEventHandler)
```
```csharp
var result = await DialogHost.Show(
content: new ConfirmDialog(),
dialogIdentifier: "MainWindow",
openedEventHandler: (s, e) => Console.WriteLine("Opened"),
closingEventHandler: (s, e) => {
if (e.Parameter == null) e.Cancel();
}
);
```
--------------------------------
### DialogContent Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets the content to display in the dialog. This can be a control or a view model and is shown when IsOpen is true or when no content is passed to Show().
```csharp
public object? DialogContent { get; set; }
```
--------------------------------
### Open Dialog with Event Handlers
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Shows how to open a dialog and attach event handlers for when the dialog is opened and closing. The closing event handler can be used to cancel the dialog closure.
```csharp
var result = await DialogHost.Show(content,
openedEventHandler: (s, e) => { },
closingEventHandler: (s, e) => { e.Cancel(); }
```
--------------------------------
### Show() - With Closing Handler
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Shows a modal dialog and invokes a provided callback function just before the dialog closes.
```APIDOC
## Show() - With Closing Handler
### Description
Shows a modal dialog and invokes a callback when the dialog is closing.
### Method
`public static Task Show(object? content, DialogClosingEventHandler closingEventHandler)`
### Parameters
#### Path Parameters
- `content` (object?) - Optional - Content to display
- `closingEventHandler` (DialogClosingEventHandler) - Required - Callback invoked before dialog closes
### Response
#### Success Response (Task)
- Returns `Task` - result from the close parameter
### Request Example
```csharp
var result = await DialogHost.Show(content, (s, args) => {
if (args.Parameter == null) {
args.Cancel(); // Require a result
}
});
```
```
--------------------------------
### Show Dialog and Handle Closing Event in C#
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/README.md
Use the DialogHost.Show method to display a dialog and provide a closing event handler to intercept the closing event.
```csharp
var result = await DialogHost.Show(viewOrModel, ClosingEventHandler);
```
--------------------------------
### Handle Dialog Closing Event with Cancellation
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/event-args-handlers.md
Example of subscribing to the DialogClosing event to inspect parameters and potentially cancel the close operation if no result is provided.
```csharp
DialogClosingEventHandler handler = (sender, args) => {
if (args.Parameter == null && args.CanBeCancelled) {
// User didn't provide a result - cancel the close
args.Cancel();
}
};
await DialogHost.Show(content, closingEventHandler: handler);
```
--------------------------------
### Handling 'DialogHost is already open' Error
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/getting-started.md
Shows two solutions for the 'DialogHost is already open' error: enabling multiple dialogs or explicitly closing the existing dialog before opening a new one.
```csharp
// Option 1: Enable multiple
dialogHost.IsMultipleDialogsEnabled = true;
// Option 2: Close first dialog
DialogHost.Close("MainDialogHost");
await DialogHost.Show(newContent);
```
--------------------------------
### Timeout Auto-Close Example
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialogsession-class.md
Implements an automatic dialog closing mechanism after a specified delay. It checks `IsEnded` to prevent closing an already closed session.
```csharp
var result = await DialogHost.Show(content, (sender, args) => {
Task.Delay(5000).ContinueWith(_ => {
if (!args.Session.IsEnded) {
args.Session.Close("timeout");
}
});
});
```
--------------------------------
### XAML Declarative DialogHost Usage
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Provides a basic XAML structure for integrating the DialogHost into your application, showing how to bind its IsOpen property and set the dialog content.
```xml
```
--------------------------------
### OpenDialogCommand Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets the command used to open the dialog. Can be bound to buttons in XAML. The command parameter becomes the DialogContent if no content is already set.
```APIDOC
## OpenDialogCommand Property
### Description
Gets the command used to open the dialog. Can be bound to buttons in XAML. The command parameter becomes the `DialogContent` if no content is already set.
### Type
`ICommand`
### Read-only
Yes
### Can Execute
When `IsOpen` is `false`
```
--------------------------------
### DialogClosingCallback Property
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/dialoghost-class.md
Gets or sets a callback invoked when the dialog is closing. Allows cancellation and inspection of close parameters. Alternative to subscribing to the DialogClosing event.
```APIDOC
## DialogClosingCallback Property
### Description
Gets or sets a callback invoked when the dialog is closing. Allows cancellation and inspection of close parameters. Alternative to subscribing to the `DialogClosing` event.
### Type
`DialogClosingEventHandler?`
### Default
`null`
### Binding Mode
Two-way
### Dependency Property
`DialogClosingCallbackProperty`
```
--------------------------------
### Positioning System
Source: https://github.com/avaloniautils/dialoghost.avalonia/blob/main/_autodocs/README.md
Documentation for the popup positioning system, including interfaces and implementations for dialog placement.
```APIDOC
## Positioning System
### Description
Documentation for the popup positioning system, outlining the `IDialogPopupPositioner` interface and its implementations for dialog placement.
### Method
N/A (This is a reference to a positioning system)
### Endpoint
N/A (This is a reference to a positioning system)
### Parameters
#### Interfaces
- **IDialogPopupPositioner**
- **IDialogPopupPositionerConstrainable**
#### Implementations
- **CenteredDialogPopupPositioner** (default)
- **AlignmentDialogPopupPositioner** (flexible positioning)
### Request Example
N/A
### Response
N/A
### Remarks
- Includes custom positioner examples.
```