0

In my app (C# + WinUI + Template Studio), I have an elaboration page and a left side Navigation Menu Items.

During the elaboration, I don't want that user can navigate to other pages.

What is the correct method to prevent this ?

I cannot find any example code to disable - temporarily - navigation.

I'm only capable to disable "back button" with: Frame.BackStack.Clear();

I've tried to use App Service, like: App.GetService<NavigationViewItem>().SelectsOnInvoked = false; And also numerous variations, but without success, or looking for a "cancel event" when fired the: private void OnNavigated(object sender, NavigationEventArgs e) on "ShellViewModel", but cannot find it.

Thanks in advance for any suggestion.

PIGRECO
  • 3
  • 1

1 Answers1

0

You can just disable the NavigationView like this.

NavigationViewSerivice.cs

private async Task DoElaboration()
{
    if (_navigationView is not null)
    {
        _navigationView.IsEnabled = false;
        await Task.Delay(1000);
        _navigationView.IsEnabled = true;
    }
}

private async void OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
    if (args.IsSettingsInvoked)
    {
        _navigationService.NavigateTo(typeof(SettingsViewModel).FullName!);
    }
    else
    {
        await DoElaboration();

        var selectedItem = args.InvokedItemContainer as NavigationViewItem;

        if (selectedItem?.GetValue(NavigationHelper.NavigateToProperty) is string pageKey)
        {
            _navigationService.NavigateTo(pageKey);
        }
    }
}
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
  • Frankly, I was hoping there was a more orthodox solution. Is it wrong - as an alternative - to use a static boolean property (in "NavigationViewService.cs") and exit from "OnItemInvoked" when it's true (example, in my "Elaboration.cs": NavigationViewService.IsNavigationDisabled = true; await DoSomething(); NavigationViewService.IsNavigationDisabled = false;) ? Thanks for the answer (and also for your videos, they helped me a lot). – PIGRECO Feb 09 '23 at 20:55
  • Thanks for watching my videos! Regarding to the alternative solution, First, I would try to avoid using static as flags at least. It's not a good practice in my opinion. Second, if you don't disable the ``NavigationView``, the user will be able to click items while ``DoElaboration()`` and ``OnItemInvoked()`` will be called while it shouldn't be. Third, disabling the ``NavigationView``, tell the user that "you can't navigate for the moment". – Andrew KeepCoding Feb 10 '23 at 09:39