dotnet / dotnet/maui

XAML Source Generation doesn't check for null references

Open
#36,313 2 comments 3 reactions 2 assignees Claimed by @simonrozsival View on GitHub
area-xaml platform/android s/triaged s/verified t/bug xsg
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

This means that bindings where intermediate properties are null will cause NullReferenceExceptions instead of being silently tolerated as runtime and XamlC bindings did in the past.

This makes it unnecessarily incompatible with many existing codebases. Switching to XAML Source Generation is too much overhead because every binding in every page needs to be verified at runtime.

Emitted code like this:

```c#
getter: source => (source.A.B.C.Text, true),
```

could easily be A?.B?.C instead, without a significant performance loss.

At the very least, an option could be provided to generate code with null checks. Or syntax like `{Binding A?.B?.C?.Text}` could be introduced.

### Steps to Reproduce

1. Run the repro app. It will crash with a NullReferenceException.

### Link to public reproduction project repository

https://github.com/mfeingol/repros/tree/main/MauiSourceGenRepro

### Version with bug

10.0.80

### Is this a regression from previous behavior?

No, this is something new

### Last version that worked well

Unknown/Other

### Affected platforms

Android

### Affected platform versions

Android 16

### Did you find any workaround?

Remain on XamlC

### Relevant log output

```shell

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.