当用户通过 X 关闭应用程序时显示消息对话框

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

如何在用户通过右上角的X关闭UWP应用程序时显示消息对话框?

c# uwp
2个回答
0
投票

唯一的问题是,如果我在消息对话框中单击“中止”,我就不能 再次使用按钮

如果您想在ContentDialog中多次使用该按钮,您可以使用xaml文件创建一个新的ContentDialog并将该按钮添加到xaml中。

右键单击您的项目 -> 添加 -> 新项目 -> 选择内容对话框

confirmDialog.xaml

<ContentDialog
    x:Class="UWPCloseTest.confirmDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPCloseTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Tip"
    PrimaryButtonText="OK"
    SecondaryButtonText="Cancel"
    PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
    SecondaryButtonClick="ContentDialog_SecondaryButtonClick">

    <Grid>
        <StackPanel>
            <Button Content="Test Button"/>
            <TextBlock Text="Are you sure you want to close it?"/>
        </StackPanel>
    </Grid>
</ContentDialog>

MainPage.xaml.cs

public MainPage()
{
    this.InitializeComponent();

    Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += async (sender, args) =>
    {
        args.Handled = true; // Prevent the default close behavior
    
        confirmDialog confirmDialog = new confirmDialog();
        var result = await confirmDialog.ShowAsync();
       

        if (result == ContentDialogResult.Primary)
        {
            App.Current.Exit(); // Close the app
        }
        
    };
}

0
投票

这是代码

her is the code  public MainPage()
        {
            this.InitializeComponent();
            //Följande kod kommer från tråden https://stackoverflow.com/questions/43414593/how-to-change-the-app-title-in-a-uwp-app
            var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
            appView.Title = "Namlös.txt";

            //Kommer från tråden https://stackoverflow.com/questions/35056464/uwp-on-desktop-closed-by-top-x-button-no-event
            SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += this.OnCloseRequest;
        }

        //Lyssnare som kollar om användaren trycker på X uppe i högra hörnet
        //Följande funktion kommer från tråden https://stackoverflow.com/questions/35056464/uwp-on-desktop-closed-by-top-x-button-no-event
        private async void OnCloseRequest(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
        {
            myText.Text = "Deferral";
            var defferal = e.GetDeferral();
            var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();

            //Splittar titeln för att kolla om filen är sparad eller inte 
            string[] checkTitle = appView.Title.Split(".txt");

            //Kollar om filen inte är sparad
            if (checkTitle[checkTitle.Length - 1] == "*")
            {
                //Visar en medelanderuta med medelande "Spara fil?"
                MessageDialog message = new MessageDialog("Spara fil?");

                //Ja för att spar fil
                message.Commands.Add(new UICommand("Ja", async x =>
                {
                    File(null);
                    defferal.Complete();

                }));

                //Nej för att inte spara fil
                message.Commands.Add(new UICommand("Nej", x =>
                {
                    defferal.Complete();

                }));

                //Avbryt för att hindra att programmet stängs ned och återgå till föregående text
                message.Commands.Add(new UICommand("Avbryt", x =>
                {
                    myText.Text = "Abryt";
                }));

                //Visar rutan 
                await message.ShowAsync();
            }
            else
            {
                    defferal.Complete();

            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.