简体   繁体   中英

HOW TO: Get the Item Selected in NavigationView in C++

I'm new to UWP and I have what I think is a simple problem to solve, but the examples I've found haven't worked. I'm using a navigation view in UWP project and I want to be able to switch page and display in a frame. When I select an item in the navigation view the ItemInvoked event is fired. I know the code to load the page into the frame which I've included below.

void enVigilServer::MainPage::nvSample_ItemInvoked(Windows::UI::Xaml::Controls::NavigationView^ sender, Windows::UI::Xaml::Controls::NavigationViewItemInvokedEventArgs^ args)
{

this->contentFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(SysConf::typeid));

}

My problem is how to determine which item I've selected from the NavigationView to show the relevant page.

Thanks

It's actually related to c++-cx and I will purpose this tag to your issue.

To make the navigation simple we can use the tag system in our app. See the following code:

    <NavigationView x:Name="NavigationViewControl" ItemInvoked="NavigationViewControl_ItemInvoked" >
        <NavigationView.MenuItems>
            <NavigationViewItem Content="A" x:Name="A" Tag="tga" />
            <NavigationViewItem Content="B" x:Name="B" Tag="tgb"/>
            <NavigationViewItem Content="C" x:Name="C" />
        </NavigationView.MenuItems>
        <Frame x:Name="contentFrame"/>
    </NavigationView>

This is an example, we will add tags to our items. Then we will do the following in our invoke code:

void NavigationVWCX::MainPage::NavigationViewControl_ItemInvoked(Windows::UI::Xaml::Controls::NavigationView^ sender, Windows::UI::Xaml::Controls::NavigationViewItemInvokedEventArgs^ args)
{
    auto navitemtag = args->InvokedItemContainer->Tag->ToString();
    if (navitemtag == "tga")
    {
        contentFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(PageA::typeid));
    }
    if (navitemtag == "tgb")
    {
        contentFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(PageB::typeid));
    }   
}

BTW, don't forget to also add tag to your pages, like:

PageB::PageB()
{
    InitializeComponent();
    this->Tag = "tgb";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM