microsoft / microsoft/microsoft-ui-xaml

Wrong exception thrown when navigating a frame to an invalid type

Open
#11,064 0 comments 0 reactions 0 assignees View on GitHub
area-ErrorHandling area-Navigation bug needs-triage
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

If I call `Frame.Navigate(Type)`, but that type is not of type `Page`, I'm getting a NullReferenceException:

### Why is this important?

It's a poor developer experience that doesn't help the user realize the mistake and move past it.

### Steps to reproduce the bug

1. Add a Frame to your Window
2. Add a UserControl to the project
3. In code-behind call MyFrame.Navigate(typeof(MyUserControl))`
4. Run and observe the null-reference exception.
Repro app: [App14.zip](https://github.com/user-attachments/files/26720656/App14.zip)

### Actual behavior

Unhelpful/incorrect null reference exception

### Expected behavior

Exception message that is actually helpful. For example, telling me that the parameter must be of type Page.

### Screenshots

Image

### NuGet package version

1.8.260317003

### Windows version

Windows 11 (24H2): Build 26100

### Additional context

From feature suggestion logged in WinUIEx: https://github.com/dotMorten/WinUIEx/issues/260

With .NET you could also use generics to help guide the developer while writing:

```csharp
public static bool Navigate(this Frame frame) where T : Page
=> frame.Navigate(typeof(T));

public static bool Navigate(this Frame frame, object parameter) where T : Page
=> frame.Navigate(typeof(T), parameter);

public static bool Navigate(this Frame frame, object? parameter, NavigationTransitionInfo info) where T : Page
=> frame.Navigate(typeof(T), parameter, info);
```
Also in WinUIEx 2.9.1 I added an analyzer to help call out invalid uses of the Navigate call during compile time.

Contributor guide

Open the contributing guide

Research direction

Start at the Frame.Navigate(Type) entry point and use the linked App14.zip reproduction to observe the failure when the type is a UserControl rather than a Page. Trace the resulting exception and add coverage for this invalid input if the surrounding tests provide a suitable location. Done means the call reports that the parameter must be a Page instead of throwing a NullReferenceException.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.