microsoft / microsoft/CsWinRT

MapChanged Event Raises Null args in WinUI 3 with ApplicationData.LocalSettings.Values

Open
#1,988 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

Summary:
In a WinUI 3 app using ApplicationData.Current.LocalSettings.Values.MapChanged, the MapChanged event occasionally fires with a null args parameter, causing a NullReferenceException when accessing args.CollectionChange.

Expected behavior:
The IMapChangedEventArgs parameter (args) should never be null. All values should be non-null per WinRT metadata contracts.

Environment:

WinUI version: Microsoft.WindowsAppSDK 1.5 (or applicable version)

SDK / runtime: Microsoft.Windows.SDK.NET, WinRT.Runtime

.NET version: .NET 8

Platform: Windows 11

Architecture: x64

Repro Steps:

Subscribe to MapChanged:

ApplicationData.Current.LocalSettings.Values.MapChanged += HandleBackingPropertySetChanged;

Assign a value:

ApplicationData.Current.LocalSettings.Values["Temperature"] = 0.75;

In your handler:

private void HandleBackingPropertySetChanged(
    IObservableMap<string, object> sender,
    IMapChangedEventArgs<string> args)
{
    // This line throws unexpectedly
    switch (args.CollectionChange) { ... }
}

Observe args == null and resulting NullReferenceException.

Notes:

args is normally a WinRT-projected value and should never be null.

This seems to originate from Do_Abi_Invoke(...) in the auto-generated interop.

Bug occurs only occasionally, likely due to ABI event wiring edge case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Do_Abi_Invoke(...) path in the auto-generated interop used by ApplicationData.Current.LocalSettings.Values.MapChanged, then reproduce the event with the provided handler and assignment on Windows 11. Done means the MapChanged callback does not receive null IMapChangedEventArgs and accessing args.CollectionChange no longer causes a NullReferenceException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.