Avalonia UI
首页支持GitHub 仓库English Doc
  • 👋欢迎
  • 文档
    • ⚡快速开始
      • IDE 支持
        • JetBrains Rider 设置
      • 使用 Avalonia 开发
        • Model-View-ViewModel 模式(MVVM)
        • 控件和布局
        • 数据绑定
        • 图像和动画
      • Windows
      • UserControls
      • 资产
      • 开发者工具
      • 错误和警告日志
      • 未处理的异常
      • 应用生命周期
    • 🔁数据绑定
      • 数据上下文
      • 变化通知
      • 绑定
      • 编译绑定
      • 与控件绑定
      • 转换绑定值
      • 绑定到命令
      • 绑定到任务和可观察对象
      • 使用代码进行绑定
      • 在控件模板中实现绑定
      • 绑定Classes
      • 创建和绑定到附加属性
      • Data Validation
    • 🎨样式
      • 样式
      • 选择器
      • 资源
      • 疑难解答
    • 🧰控件
      • AutoCompleteBox
      • Border
      • Buttons
        • Button
        • RepeatButton
        • RadioButton
        • ToggleButton
        • ButtonSpinner
        • SplitButton
        • ToggleSplitButton
      • Calendar
      • Canvas
      • Carousel
      • CheckBox
      • ComboBox
      • ContentControl
      • ContextMenu
      • Decorator
      • DataGrid
        • DataGridColumns
      • DatePicker
      • DockPanel
      • Expander
      • Flyouts
      • Grid
      • GridSplitter
      • Image
      • ItemsControl
      • ItemsRepeater
      • LayoutTransformControl
      • ListBox
      • MaskedTextBox
      • Menu
      • NativeMenu
      • NumericUpDown
      • Panel
      • ProgressBar
      • RelativePanel
      • ScrollBar
      • ScrollViewer
      • Separator
      • Slider
      • SplitView
      • StackPanel
      • TabControl
      • TabStrip
      • TextBlock
      • TrayIcon
      • TreeDataGrid
        • Creating a Hierarchical TreeDataGrid
        • Creating a Flat TreeDataGrid
        • TreeDataGrid column types
      • TimePicker
      • TextBox
      • ToolTip
      • TreeView
      • TransitioningContentControl
      • UserControl
      • Viewbox
      • Window
      • WrapPanel
    • 📚模板
      • 数据模板
      • 在代码中创建数据模板
      • 实现 IDataTemplate 接口
    • ✏️自定义控件
      • 控件类别
      • 定义属性
    • 🖱️输入
      • 路由事件
      • 剪贴板
      • 鼠标与触控设备
      • 快捷键
    • 🔑动画
      • 关键帧动画
      • 过渡
      • 页面过渡
    • 📐布局
      • 面板概述
      • Alignment、Margin 和 Padding
      • 创建自定义面板
    • 📦发布/分发
      • macOS
  • API 参考
    • 🗒️命名空间
      • Avalonia
      • Avalonia.Animation
        • Avalonia.Animation.Easings
        • Avalonia.Animation.Animators
      • Avalonia.Collections
      • Avalonia.Controls
      • Avalonia.Data
        • Avalonia.Data.Core.Plugins
        • Avalonia.Data.Core
        • Avalonia.Data.Converters
      • Avalonia.Diagnostics
      • Avalonia.Dialogs
  • 指南
    • 🐣基础
      • XAML 介绍
      • Code-behind
      • MVVM 架构
      • 在UI线程上操作
    • 🤿深入
      • 在树莓派上运行你的应用
      • 在树莓派上运行你的应用(使用Raspbian Lite)
      • ReactiveUI
        • 视图激活机制
        • 路由
        • 数据持久化
        • 绑定到 Sorted/Filtered 数据
    • 👩‍💻👩💻 开发人员指南
      • 🏭从源代码中构建 Avalonia
      • Avalonia 与 WPF 和 UWP 之间的比较
      • Debugging Previewer
      • Debugging the XAML compiler
      • macOS 开发
      • Release Process
      • 维护稳定的分支
  • 教程
    • 📋待办事项应用
    • 📻音乐商店应用
    • 🕸️在浏览器中运行
    • 📱为移动设备开发
  • 杂项
    • 👪社区
    • 🖥️WPF 开发者建议
    • 📋正在使用 Avalonia 的项目
    • ❔常见问题
由 GitBook 提供支持
在本页
  • Common Properties
  • Reference
  • Source code
  • The main window
  • Show, hide and close a window
  • Show a window as a dialog
  • Prevent a window from closing

这有帮助吗?

在GitHub上编辑
  1. 文档
  2. 控件

Window

上一页Viewbox下一页WrapPanel

最后更新于2年前

这有帮助吗?

is a top-level .

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 .

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

Source code

The main window

The main window is the window passed to ApplicationLifetime.MainWindow in the OnFrameworkInitializationCompleted method of your your App.axaml.cs file:

public override void OnFrameworkInitializationCompleted()
{
    if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
    {
        desktopLifetime.MainWindow = new MainWindow();
    }
}

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:

var window = new MyWindow();
window.Show();

Windows can be closed using the Close method. This has the same effect as when a user clicks the window's close button:

window.Close();

// A closed window cannot be shown.
window.Show();

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:

window.Hide();

// Window can now be shown again later
window.Show();

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:

// Here we assume this code is executed from our current Window class and "this" object is a Window.
// Alternatively you can get global MainWindow from Application.ApplicationLifetime casted to IClassicDesktopStyleApplicationLifetime.
var ownerWindow = this;
var window = new MyWindow();
window.ShowDialog(ownerWindow);

The ShowDialog method will return immediately. If you want to wait for the dialog to be closed, you can await the call:

var window = new MyWindow();
await window.ShowDialog(ownerWindow);

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:

public class MyDialog : Window
{
    public MyDialog()
    {
        InitializeComponent();
    }

    private void OkButton_Click(object sender, EventArgs e)
    {
        Close("OK Clicked!");
    }
}
var dialog = new MyDialog();

// The result is a string so call `ShowDialog<string>`.
var result = await dialog.ShowDialog<string>(ownerWindow);

Prevent a window from closing

A window can be prevented from closing by handling the Closing event and setting e.Cancel = true:

window.Closing += (s, e) =>
{
    e.Cancel = true;
};

You could also hide the window instead. This allows the window to be re-shown after the user clicks the close button:

window.Closing += (s, e) =>
{
    ((Window)s).Hide();
    e.Cancel = true;
};

See also

🧰
Window
ContentControl
quickstart
Window
Window.cs
Prevent a window from closing