Can anyone please tell me why the IMapChangedEventArgs value always returns null in a C# UWP app targeting Windows 10, Version 2004 (10.0; Build 19041)?
I am trying to handle a MapChanged event raised by a ApplicationDataContainerSettings object with a handler having this signature
SettingsMapChanged(IObservableMap<string, object> sender, IMapChangedEventArgs<string> @event)
The debug output of the code below is:
[One] = One
Null argument
Empty Map
Null argument
Empty Map
Null argument
[Two] = Two
This shows that sender correctly returns a reference to the the updated map but the MapChangedEventArgs value is always null. I've tried alternatives to casting to ApplicationDataContainerSettings but without success. Any suggestions would be very welcome.
and here:
using System.Diagnostics;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
namespace AppDataTest
{
public sealed partial class MainPage : Page
{
const string ContainerName = "Settings";
const string Key1 = "One";
const string Key2 = "Two";
ApplicationDataContainerSettings Settings;
public MainPage()
{
this.InitializeComponent();
ApplicationDataContainer userSettings = ApplicationData.Current.LocalSettings.CreateContainer(ContainerName, ApplicationDataCreateDisposition.Always);
Settings = (ApplicationDataContainerSettings)userSettings.Values;
Settings.Clear();
Settings.MapChanged += SettingsMapChanged;
Settings.Add(Key1, "One");
Settings.Clear();
Settings[Key2] = "Two";
}
private void SettingsMapChanged(IObservableMap<string, object> sender, IMapChangedEventArgs<string> @event)
{
if (@event == null)
{
Debug.WriteLine("Null argument");
}
else
{
Debug.WriteLine($"{@event.CollectionChange} : {@event.Key}");
}
Debug.WriteLineIf(sender.Count == 0, "Empty Map");
foreach (var item in sender)
{
Debug.WriteLine($"[{item.Key}] = {item.Value}");
}
}
}
}
Many thanks