WinUI 3 NavigationService not navigating to ShellPage after successful login

TUAN THANH VO 0 Reputation points
2024-10-27T05:36:03.9466667+00:00

I'm developing a WinUI 3 app using Template Studio with a Navigation Bar. The INavigationService has a NavigateTo method that I can use to navigate between custom pages like Login and SignUp. However, I'm having an issue where I can't navigate to ShellPage after a successful login.

Specifically, the following code doesn't work as expected:


private readonly INavigationService _navigationService;

private UIElement? _shell;

public LoginViewModel(IAuthService authService, INavigationService navigationService)

{

    _authService = authService;

    _navigationService = navigationService;

}

if (loginSuccessful)

{

    // Not working in this context

    _navigationService.NavigateTo(typeof(ShellViewModel).FullName!);

    // Works but doesn't show full content of ShellPage (doesn't automatically activate BlankPage)

    var shell = App.GetService<ShellPage>();

    App.MainWindow.Content = shell ?? (UIElement)new Frame();

}

Interestingly, if I use NavigateTo with custom pages like SignUp, it works fine:


public void NavigateToSignUp()

{

    _navigationService.NavigateTo(typeof(SignUpViewModel).FullName!);

}

When i'm using:

// Works but doesn't show full content of ShellPage (doesn't automatically activate BlankPage)    var shell = App.GetService<ShellPage>(); 
App.MainWindow.Content = shell ?? (UIElement)new Frame();

I got:
User's image
But i'm expect:
User's image
Question: How can I successfully navigate to ShellPage after login and ensure that ShellPage displays its full content

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,915 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
794 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,006 questions
{count} votes

1 answer

Sort by: Most helpful
  1. TUAN THANH VO 0 Reputation points
    2024-10-29T03:50:20.6933333+00:00

    I've solve my problem by using this

    if (loginSuccessful)
    {
        var shell = App.GetService<ShellPage>();
        App.MainWindow.Content = shell;
        // Get the frame from the shell
        if (shell.FindName("NavigationFrame") is Frame shellFrame)
        {
            _navigationService.Frame = shellFrame;
            _navigationService.NavigateTo(typeof(AccountViewModel).FullName!);
        }
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.