Search code examples
c#winui-3windows-template-studio

C# WinUI TemplateStudio: how to prevent navigation during elaboration


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.


Solution

  • 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);
            }
        }
    }