0
votes

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.

The API specs are here: https://docs.microsoft.com/en-us/uwp/api/windows.storage.applicationdatacontainersettings?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev16.query%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(Windows.Storage.ApplicationDataContainerSettings);k(TargetFrameworkMoniker-.NETCore,Version%3Dv5.0);k(DevLang-csharp)%26rd%3Dtrue%26f%3D255%26MSPPError%3D-2147217396&view=winrt-19041

and here:

https://docs.microsoft.com/en-us/uwp/api/windows.foundation.collections.iobservablemap-2.mapchanged?view=winrt-19041

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

1

1 Answers

0
votes

The definition of the MapChanged event is:

public event MapChangedEventHandler<string,object> MapChanged;

Its event parameter is object type (not IMapChangedEventArgs<string>), according to the test, it always returns null.

This method is designed to notify when the settings changes, but does not include specific modification data.

So in this event, you need to judge the items of the collection based on the sender.