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.
Why do i get this error
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 this
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="" />
</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;
}
}
}
2 answers
Sort by: Most helpful
-
Michael Taylor 54,811 Reputation points
2024-08-18T02:51:56.3+00:00 -
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.