c++winrt-xamldesktop-applicationwinui-3c++-winrt

How to navigate to a page in NavigationView in WinUI3 using C++


I am working on very simple demo of NavigationView in WinUI3 using C++.enter image description here

It is very unfortunate that there is no material or tutorial available for this.

<Window
    x:Class="App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <NavigationView x:Name="NavView"
                    IsTitleBarAutoPaddingEnabled="False"
                    IsTabStop="False" IsBackButtonVisible="Collapsed" PaneDisplayMode="Left">

        <NavigationView.MenuItems>
            <NavigationViewItem Icon="Page2" Content="Tab1" Tag="Tab1" IsSelected="True"/>
            <NavigationViewItem Icon="Page2" Content="Tab2" Tag="Tab2"/>
        </NavigationView.MenuItems>

        <ScrollViewer>
            <Frame x:Name="ContentFrame" Padding="12,0,12,24" IsTabStop="True"/>
        </ScrollViewer>

    </NavigationView>
</Window>

How to navigate to a Page on NavigationView using C++?

ContentFrame().Navigate(**WHAT CODE GOES HERE**);

I already know how to do it using C#. and I am not using UWP with c++/winrt.

I need solution for WinUI3 using c++


Solution

  • Check out the C++/WinRT sample code in the docs for NavigationView on MSDN (you can search for void MainPage::NavView_Navigate inside the page to quickly find it).

    Here is a summary:

    Windows::UI::Xaml::Interop::TypeName pageTypeName =
      winrt::xaml_typename<NavigationViewCppWinRT::SettingsPage>();
    
    ContentFrame().Navigate(pageTypeName, nullptr, transitionInfo);
    
    // transitionInfo is passed to your event handler
    // if you do this without a transition info, simply
    // omit the last argument.