Why do i get this error

Wannes Aneca 0 Reputation points
2024-08-17T19:58:33.26+00:00

When i start debugging and i press a NavigationMenuItem, i get this error (See Screenshot).

the NavigationPages are in the folder NavigationMenu.
Does anyone know how to fix thisScreenshot 2024-08-17 215158

My code is

MainPage.xaml

<UserControl

x:Class="Phone_Connection.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="using:Phone_Connection"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d">

<Grid>

    <NavigationView x:Name="NavView"

                    IsBackButtonVisible="Collapsed"

                    IsPaneOpen="True"

                    SelectionChanged="NavigationView_SelectionChanged">

        <NavigationView.MenuItems>

            <NavigationViewItem Icon="Home" Content="Home" Tag="Home"/>

            <NavigationViewItem Icon="CellPhone" Content="Phone" Tag="Phone"/>

            <NavigationViewItem Icon="Audio" Content="Audio" Tag="Audio"/>

            <NavigationViewItem Content="AirPlay" Tag="AirPlay">

                <NavigationViewItem.Icon>

                    <FontIcon Glyph="&#xE95A;" />

                </NavigationViewItem.Icon>

            </NavigationViewItem>

        </NavigationView.MenuItems>

        <Frame x:Name="contentFrame"/>

    </NavigationView>

</Grid>
```</UserControl>  
  
MainPage.xaml.cs  
  
using Microsoft.UI.Xaml;

using Microsoft.UI.Xaml.Controls;

using Phone_Connection.NavigationMenu;

namespace Phone_Connection

{

public sealed partial class MainPage : UserControl

{

public MainPage()

{

    this.InitializeComponent();

    contentFrame = new Frame();

}

private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)

{

    if (args.SelectedItem is NavigationViewItem selectedItem)

    {

        var tag = selectedItem.Tag.ToString();

        NavigateTo(tag);

    }

}

private void NavigateTo(string pageName)

{

    // Zorg ervoor dat contentFrame niet null is

    if (contentFrame == null)

    {

        // Mogelijk een foutmelding of logica om te controleren waarom contentFrame null is

        return;

    }

    // Load the content based on the pageName

    switch (pageName)

    {

        case "Home":

            contentFrame.Navigate(typeof(NavigationMenu.HomePage)); // Zorg ervoor dat HomePage bestaat

            break;

        case "Phone":

            contentFrame.Navigate(typeof(NavigationMenu.PhonePage)); // Zorg ervoor dat PhonePage bestaat

            break;

        case "Audio":

            contentFrame.Navigate(typeof(NavigationMenu.AudioPage)); // Zorg ervoor dat AudioPage bestaat

            break;

        case "AirPlay":

            contentFrame.Navigate(typeof(NavigationMenu.AirPlayPage)); // Zorg ervoor dat AirPlayPage bestaat

            break;

    }

}

}


Universal Windows Platform (UWP)
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,011 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,896 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 54,811 Reputation points
    2024-08-18T02:51:56.3+00:00

    In that exception dialog, click the stack trace and look at it carefully. The base code you showed appears fine but the Navigate method may be throwing an exception. The stack trace will help clarify this.

    0 comments No comments

  2. Junjie Zhu - MSFT 18,501 Reputation points Microsoft Vendor
    2024-08-19T02:35:35.91+00:00

    Hi @Wannes Aneca ,

    Welcome to Microsoft Q&A!

    I can't reproduce this error using your code, but the homepage is not displayed.

    According to the official sample, there is no need to initialize the Frame again on the code side. It is recommended to delete contentFrame = new Frame();.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.