1、效果演示
2、在Views中创建WindowDemo.axaml如下图
WindowDemo.axaml代码如下
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="200"
Width="200"
Height="200"
x:Class="AvaloniaStepByStepLearning.WindowDemo"
WindowStartupLocation="CenterOwner"
Title="WindowDemo">
<Button x:Name="btnClose" Click="BtnClose_OnClick" HorizontalAlignment="Center" VerticalAlignment="Center" Content="关闭窗口"/>
</Window>
WindowDemo.axaml.cs代码如下:
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
namespace AvaloniaStepByStepLearning;
public partial class WindowDemo : Window
{
public WindowDemo()
{
InitializeComponent();
}
private void BtnClose_OnClick(object? sender, RoutedEventArgs e)
{
Close("返回的数据");
}
}
3、示例代码步骤
3.1、MainView.axaml添加代码如下
<Button HorizontalAlignment="Center" Content="Menu/菜单">
<Button.Flyout>
<MenuFlyout>
<MenuItem x:Name="MItOpen" Header="打开" Click="MenuItem_OnClick"/>
<MenuItem Header="-"/>
<MenuItem x:Name="MItClose" Header="关闭" Click="MenuItem_OnClick"/>
<MenuItem Header="-"/>
<MenuItem x:Name="MItHideAndShow" Header="隐藏/显示" Click="MenuItem_OnClick"/>
</MenuFlyout>
</Button.Flyout>
</Button>
<Button x:Name="btnGetString" HorizontalAlignment="Center" Content="MenuDialog" Margin="0 15 0 0">
<Button.Flyout>
<MenuFlyout>
<MenuItem x:Name="MItOpenDialog" Header="打开无返回值" Click="MenuItem_OnClick"/>
<MenuItem x:Name="MItOpenGetDialog" Header="打开获取返回值" Click="MenuItem_OnClick"/>
<MenuItem Header="-"/>
<MenuItem x:Name="MItCloseDialog" Header="关闭" Click="MenuItem_OnClick"/>
</MenuFlyout>
</Button.Flyout>
</Button>
3.2、MainView.axaml.cs添加代码如下
public Window currenWindow
{
get
{
return GetParentWindow();
}
}
public Window GetParentWindow()
{
if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
return desktop.MainWindow;
}
else
{
return null;
}
}
private WindowDemo _windowDemo;
private async void MenuItem_OnClick(object? sender, RoutedEventArgs e)
{
MenuItem _menuItem = (MenuItem)sender;
switch (_menuItem.Name)
{
case "MItOpen":
_windowDemo = new WindowDemo();
_windowDemo.Show();
break;
case "MItClose":
_windowDemo.Close();
_windowDemo = null;
break;
case "MItHideAndShow":
if (_windowDemo==null)
{
_windowDemo = new WindowDemo();
}
_windowDemo.Hide();
Thread.Sleep(3000);
_windowDemo.Show();
break;
case "MItOpenDialog":
if (_windowDemo!=null)
{
_windowDemo.Close();
_windowDemo = null;
}
_windowDemo = new WindowDemo();
await _windowDemo.ShowDialog(currenWindow);
break;
case "MItOpenGetDialog":
_windowDemo = new WindowDemo();
var str=await _windowDemo.ShowDialog<string>(currenWindow);
btnGetString.Content = str;
Debug.WriteLine(str);
break;
case "MItCloseDialog":
if (_windowDemo!=null)
{
_windowDemo.Close();
_windowDemo = null;
}
break;
default:
break;
}
}
3.3、Avalonia 获取MainWindow.axaml 代码如下
public Window currenWindow
{
get
{
return GetParentWindow();
}
}
public Window GetParentWindow()
{
if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
return desktop.MainWindow;
}
else
{
return null;
}
}