Window
​Window is a top-level ContentControl.
You will not usually create instances of the Window class directly; instead the Window class is usually sub-classed for each type of window to be shown by an application. For information on how to create new window classes from templates see the quickstart.

Common Properties

Property
Description
Title
The window title
Icon
The window icon
SizeToContent
Describes the window's auto-sizing behavior
WindowState
The minimized/maximized state of the window

Reference

​Window​

Source code

​Window.cs​

The main window

The main window is the window passed to ApplicationLifetime.MainWindow in the OnFrameworkInitializationCompleted method of your your App.axaml.cs file:
1
public override void OnFrameworkInitializationCompleted()
2
{
3
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
4
{
5
desktopLifetime.MainWindow = new MainWindow();
6
}
7
}
Copied!
It can be retrieved at any time by casting Application.ApplicationLifetime IClassicDesktopStyleApplicationLifetime.
Mobile and browser platforms don't have a concept of Window in Avalonia. Instead you need to set MainView control in Application.ApplicationLifetime when it implements ISingleViewApplicationLifetime interface.

Show, hide and close a window

You can show a window using the Show method:
1
var window = new MyWindow();
2
window.Show();
Copied!
Windows can be closed using the Close method. This has the same effect as when a user clicks the window's close button:
1
window.Close();
2
​
3
// A closed window cannot be shown.
4
window.Show();
Copied!
Note that once a window has been closed, it cannot be shown again. If you want to re-show the window then you should use the Hide method:
1
window.Hide();
2
​
3
// Window can now be shown again later
4
window.Show();
Copied!

Show a window as a dialog

You can show a window as a modal dialog by calling the ShowDialog method. ShowDialog requires an owner window to be passed:
1
// Here we assume this code is executed from our current Window class and "this" object is a Window.
2
// Alternatively you can get global MainWindow from Application.ApplicationLifetime casted to IClassicDesktopStyleApplicationLifetime.
3
var ownerWindow = this;
4
var window = new MyWindow();
5
window.ShowDialog(ownerWindow);
Copied!
The ShowDialog method will return immediately. If you want to wait for the dialog to be closed, you can await the call:
1
var window = new MyWindow();
2
await window.ShowDialog(ownerWindow);
Copied!
Dialogs can return a result by calling the Close method with an object. This result can then be read by the caller of ShowDialog. For example:
1
public class MyDialog : Window
2
{
3
public MyDialog()
4
{
5
InitializeComponent();
6
}
7
​
8
private void OkButton_Click(object sender, EventArgs e)
9
{
10
Close("OK Clicked!");
11
}
12
}
Copied!
1
var dialog = new MyDialog();
2
​
3
// The result is a string so call `ShowDialog<string>`.
4
var result = await dialog.ShowDialog<string>(ownerWindow);
Copied!

Prevent a window from closing

A window can be prevented from closing by handling the Closing event and setting e.Cancel = true:
1
window.Closing += (s, e) =>
2
{
3
e.Cancel = true;
4
};
Copied!
You could also hide the window instead. This allows the window to be re-shown after the user clicks the close button:
1
window.Closing += (s, e) =>
2
{
3
((Window)s).Hide();
4
e.Cancel = true;
5
};
Copied!
Last modified 1mo ago