michaelscodingspot / WPF_MVVMC

A WPF framework for navigation between pages with MVC-like pattern
MIT License
64 stars 18 forks source link

Capture app closing event from page code-behind #17

Closed papillon2k2 closed 1 week ago

papillon2k2 commented 11 months ago

In one of the steps of the wizard I am doing a long process, and if the user tries to close app during the process, I want to show a confirm dialog box to ask for confirmation. How could this be done?

michaelscodingspot commented 11 months ago

Hi , didn’t have time to work but I’ll answer tomorrow

On Sun, 6 Aug 2023 at 15:49 papillon2k2 @.***> wrote:

In one of the steps of the wizard I am doing a long process, and if the user tries to close app during the process, I want to show a confirm dialog box to ask for confirmation. How could this be done?

— Reply to this email directly, view it on GitHub https://github.com/michaelscodingspot/WPF_MVVMC/issues/17, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF5WVCIBLIZ3OO3KYAQEPB3XT6HGRANCNFSM6AAAAAA3F4VO2E . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Best Regards, Michael Shpilt Software Developer | Ozcode | michaelscodingspot.com

michaelscodingspot commented 11 months ago

I think you can do this without using Regions and Controllers with something like this:

  1. Register to the close event of the window and prevent it if during long operation (see here)
  2. Show a confirm dialog. You can do this by having a UserControl in the MainWindow that's not visible unless you need to show a dialog.For nice appearance, you can create a semi-transparent background ContentControl with a big padding, and have the actual prompt take 30% of the screen or something.

So, in main window, something like this:

<Grid>
<mvvmc:Region ControllerID="MyWizard" />
<UserControl x:Name="MyDialogBox">
...
</UserControl/>
</Grid>

When the user is about to navigate:

void Window_Closing(object sender, CancelEventArgs e)
        {
            if (IsDuringLongOperation()) 
            {
                e.Cancel = true;
            }
            MyDialogBox.Visibility = Visibility.Visibile.
        }

Or just use a MessageBox like in the example.

michaelscodingspot commented 11 months ago

Just to reiterate, this problem is best not solved with MVVMC in my opinion.

papillon2k2 commented 11 months ago

thanks!