0

Let me first state how frustrated I am in not being able to find many examples that are written in C++ for UWP/WinRT. I need to be able to reference and use shared .H files, so C# is not very useful. And guess what, almost all the examples I found are in C#. I am no dummy, but I don't always know the conversion code from C# to C++.

I have Googled for the information I seek, searched for books, and read some web pages, but I have failed in finding the info I seek, so now I ask you (the coders that will know) how to solve my problem or where I might find the answers.

Here is the problem in a nutshell, I need the code for the NavigationView UI control in UWP written in C++. I followed the C# examples as best I could, but my code does not compile nor is it complete. There are 3 functions that I need to complete but I can't get them converted. I can give you any code file you want, but only give the 2 that pertain right now. Just ask if you need me to clarify something.

XAML file

    <Page
x:Class="Kcam_GUI_Test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kcam_GUI_Test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="#FFB0B0B0"
Foreground="#FF000000">


<NavigationView x:Name="NavView"
                PaneDisplayMode="Top"
                IsBackButtonVisible="Collapsed"

                SelectionChanged="NavView_SelectionChanged">

    <NavigationView.Resources>
        <SolidColorBrush x:Key="TopNavigationViewItemForegroundPointerOver" Color="Black"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundPointerOver" Color="#FF005BAF"/>
        <SolidColorBrush x:Key="TopNavigationViewItemForegroundSelected" Color="Black"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundSelected" Color="#FF005BAF"/>
        <SolidColorBrush x:Key="TopNavigationViewItemForegroundSelectedPointerOver" Color="Black"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundSelectedPointerOver" Color="#FF005BAF"/>
        <SolidColorBrush x:Key="TopNavigationViewItemForegroundPressed" Color="Black"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundPressed" Color="#FF005BAF"/>
        <SolidColorBrush x:Key="TopNavigationViewItemForegroundSelectedPressed" Color="Black"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundSelectedPressed" Color="#FF005BAF"/>
    </NavigationView.Resources>
    <NavigationView.MenuItems>
        <NavigationViewItem x:Name="OperationNav" Content="Operation" Tag="NavOperation" FontWeight="Bold"/>
        <NavigationViewItem x:Name="AdminNav" Content="Administration" Tag="NavAdmin" FontWeight="Bold"/>
        <NavigationViewItem x:Name="AdvancedNav" Content="Advanced" Tag="NavAdvanced" FontWeight="Bold"/>
        <NavigationViewItem x:Name="FactoryNav" Content="Factory" Tag="NavFactory" FontWeight="Bold"/>
    </NavigationView.MenuItems>
    <StackPanel>
        <Rectangle Height="25">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF005BAF"/>
                    <GradientStop Color="#FFB0B0B0" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <Frame x:Name="NavFrame" Margin="0,5,0,0" BorderThickness="1" BorderBrush="Black" Height="1000">
            <Slider Width="200" Height="50" Maximum="100" StepFrequency="10" TickPlacement="Inline" />
        </Frame>
  
    </StackPanel>

</NavigationView>

And just the 3 C++ functions that are causing me grief. Only one function is referenced in the XAML file.

        // This code is for when the NavigationView is clicked so it selects the correct page to navigate to.
void MainPage::NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
    if (args.IsSettingsSelected() == true)
    {
        NavView_Navigate("settings", args.RecommendedNavigationTransitionInfo());       // Notice the 1st param is not a string. Item is better, I think.
    }
    else if (args.SelectedItemContainer() != NULL)
    {
        std::string navItemTag = args.SelectedItemContainer();
        NavView_Navigate(navItemTag, args.RecommendedNavigationTransitionInfo());       // Notice the 1st param is not a string. Item is better, I think.
    }
}

// This function is responsible for loading each page. The commented code below this function was the first attempt.
void MainPage::NavView_Navigate(NavigationViewItem item, NavigationTransitionInfo transitionInfo)
{
    {
        switch (item.Tag())
        {
        case "NavOperation":
            NavFrame.Navigate(xaml_typename<Kcam_GUI_Test::OperationNavPage>());
            break;

        case "NavAdmin":
            NavFrame.Navigate(xaml_typename<Kcam_GUI_Test::AdminNavPage>());
            break;

        case "NavAdvanced":
            NavFrame.Navigate(xaml_typename<Kcam_GUI_Test::AdvancedNavPage>());
            break;

        case "NavFactory":
            NavFrame.Navigate(xaml_typename<Kcam_GUI_Test::FactoryNavPage>());
            break;
        default:
            break;
        }
    }
    
    
    /*Type _page = null;

    if (navItemTag == "settings")
    {
        _page = xaml_typename<Kcam_GUI_Test::SettingsPage>();
    }
    else
    {
        var item = _pages.FirstOrDefault(p = > p.Tag.Equals(navItemTag));
        _page = item.Page;
    }
    // Get the page type before navigation so you can prevent duplicate
    // entries in the back stack.
    var preNavPageType = ContentFrame.CurrentSourcePageType;

    // Only navigate if the selected page isn't currently loaded.
    if (!(_page is null) && !Type.Equals(preNavPageType, _page))
    {
        ContentFrame.Navigate(_page, null, transitionInfo);
    }*/
}

// This function is either C# or VB. It is what gets called when the page loads. It selects the first NavigationView.
void MainPage::NavView_Loaded(Windows::UI::Xaml::RoutedEventArgs e)
{
    // Set the initial SelectedItem.
    for each(NavigationViewItemBase item in NavView.MenuItems)                      // This line is wrong
    {
        if (item is NavigationViewItem && item.Tag.ToString() == "NavOperation")    // This line is wrong
        {
            NavView.SelectedItem = item;
            break;
        }
    }
}

I don't expect you to tell me the answers directly, but any known examples of how this is done in C++ would be helpful. And If you could recommend a book to purchase that would cover UWP/WinRT in C++ (NO C#), I would be very grateful. Thanks.

3
  • Here is navigationView document that you could refer to(code with c++ winrt). Commented Aug 16, 2021 at 5:54
  • 1
    I did finally find the C++ example code and get my code fixed, although it was buried in the link Nico gave me. I am happy now. Thanks. Commented Aug 18, 2021 at 13:16
  • Cool, if you have the solution, please feel free post your answer below, and you could mark yourself. Commented Aug 19, 2021 at 1:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.