如何编辑 MAUI Blazor Hybrid 上关闭按钮的功能

问题描述 投票:0回答:2

我正在开发一个用 MAUI Blazor 编写的应用程序。 我希望在单击红色按钮“关闭”后,弹出消息“您确定要关闭该应用程序吗?”。 如何修改Windows关闭系统按钮的功能? 我尝试使用 OnClosed 事件,但我不知道如何正确使用它。

#if WINDOWS
            events.AddWindows(wndLifeCycleBuilder =>
            {
                wndLifeCycleBuilder.OnClosed((window, args) => LogEvent("OnClosed"));

                wndLifeCycleBuilder.OnWindowCreated(window =>
                {
                    //Set size and center on screen using WinUIEx extension method
                    window.CenterOnScreen(400, 750);
                    window.ExtendsContentIntoTitleBar = true;
                });
            });

#endif

我的意思是这个按钮。

button blazor maui hybrid
2个回答
2
投票

这里

#if WINDOWS
events.AddWindows(windowsLifecycleBuilder =>
{
    windowsLifecycleBuilder.OnWindowCreated(window =>
    {
        //we need this to use Microsoft.UI.Windowing functions for our window
        var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
        var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
        var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);

        //and here it is
        appWindow.Closing += async (s, e) => 
        {
            e.Cancel = true;
            bool result = await App.Current.MainPage.DisplayAlert(
                "Alert title", 
                "You sure want to close app?", 
                "Yes", 
                "Cancel");

            if (result)
            {
                App.Current.Quit();
            }
        };
    });
});
#endif

0
投票

太棒了!这也适用于常规视觉工作室 MAUI 应用程序!

在 App.xaml.cs 中:

    protected override Window CreateWindow(IActivationState activationState)
    {
        Window window = base.CreateWindow(activationState);

#if WINDOWS
        window.Created += (s, e) =>
        {

            //we need this to use Microsoft.UI.Windowing functions for our window
            var handle = WinRT.Interop.WindowNative.GetWindowHandle(window.Handler.PlatformView);
            var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
            var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);

            //and here it is
            appWindow.Closing += async (s, e) =>
            {
                e.Cancel = true;
                bool result = await App.Current.MainPage.DisplayAlert(
                    "Alert title",
                    "You sure want to close app?",
                    "Yes",
                    "Cancel");

                if (result)
                {
                    App.Current.Quit();
                }
            };
        };
#endif
        return window;
    }
© www.soinside.com 2019 - 2024. All rights reserved.