Search code examples
c#enumsuwpdependency-propertiesargumentexception

Fixing ArgumentException in UWP enum DependencyProperty


I have the following code in a UWP app:

public sealed partial class CanvasMapControl : UserControl
{
    public static readonly DependencyProperty ActiveInteractionModeProperty =
        DependencyProperty.Register(nameof(ActiveInteractionMode),
                                    typeof(InteractionMode),
                                    typeof(CanvasMapControl),
                                    new PropertyMetadata(InteractionMode.None, CanvasMapControl_InteractionModeChanged));

    public InteractionMode ActiveInteractionMode {
        get => (InteractionMode)GetValue(ActiveInteractionModeProperty);
        set => SetValue(ActiveInteractionModeProperty, value);
    }

    private static void CanvasMapControl_InteractionModeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        ((CanvasMapControl)obj).InteractionModeInternal = (InteractionMode)args.NewValue;
    }

    // ...rest of class...
}

InteractionMode is an enum. When I attempt to run my app, I get the following exception:

System.TypeInitializationException: 'The type initializer for 'MyApp.CanvasMapControl' threw an exception.'

Inner Exception

ArgumentException: The parameter is incorrect. createDefaultValueCallback

Things I've tried:

  • Changing the DependencyProperty to an int and casting
  • Passing null in for the PropertyMetadata
  • Passing null in for the default value of the PropertyMetadata
  • Passing a function instead of a value for the default value

No matter what I try, on startup it always throws the above exception targeting the SetValue line of ActiveInteractionMode. What am I missing that is making this not work?


Update: InteractionModeInternal is a simple private property that will be used to allow me to temporarily change the ActiveInteractionMode during processing

private InteractionMode _interactionModeInternal;

private InteractionMode InteractionModeInternal
{
    get => _interactionModeInternal;
    set
    {
        _interactionModeInternal = value;
        OnInteractionModeInternalChanged(value);
    }
}

private void OnInteractionModeInternalChanged(InteractionMode interactionMode)
{
    Log.Debug($"InternalInteractionMode changed to {interactionMode}");
}

I currently have no logic driven off of it, except that it logs a string to the console.


Update 2: After further debugging, I have found the reason the crash happens on startup is that I was binding values to these properties in XAML. After commenting out those bindings, the application was able to startup, but then attempting to access either the Get or Set for ActiveInteractionMode would cause this same error with createDefaultValueCallback to occur.


Solution

  • In case anyone comes across this in the future, I'm posting this as the answer as it identifies the specific problem I was having. I don't know if I was doing something wrong, or if this was a bug either in UWP or on my phone, but I no longer have this code so cannot debug further.

    Update 2: After further debugging, I have found the reason the crash happens on startup is that I was binding values to these properties in XAML. After commenting out those bindings, the application was able to startup, but then attempting to access either the Get or Set for ActiveInteractionMode would cause this same error with createDefaultValueCallback to occur.