.NET Frameworkでは、this.close(); でウィンドウを閉じることができましたが、意外と苦戦したのでメモ。
■ MainPage.xaml.cs
namespace MauiApp;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
.
.
.
private async void OnExitClicked(object sender, EventArgs e)
{
bool ans = await DisplayAlert("確認", "アプリを終了します。よろしいですか?", "Yes", "No");
if (ans == true)
{
// アプリ(ウィンドウ)を閉じる
Application.Current.CloseWindow(GetParentWindow());
}
}
}
■ MainPage.xaml <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiApp.MainPage"> <!-- メニューバー --> <ContentPage.MenuBarItems> <MenuBarItem Text="File"> <MenuFlyoutItem Text="Exit" Clicked="OnExitClicked" Command="{Binding ExitCommand}" /> </MenuBarItem> </ContentPage.MenuBarItems> . . . </ContentPage>

開発環境:Visual Studio 2022 Community バージョン17.3
プロジェクトテンプレート:.NET MAUI アプリ
自分用覚書です。動かなかったらごめんなさい。
MenuBar からのアプリ終了方法をありがとうございました。
TabBar からのアプリ終了方法も是非ともお願いいたします。