在UWP枚举DependencyProperty中修复ArgumentException

时间:2017-05-01 16:33:48

标签: c# enums uwp dependency-properties argumentexception

我在UWP应用程序中有以下代码:

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是一个枚举。当我尝试运行我的应用程序时,我得到以下异常:

  

System.TypeInitializationException:''MyApp.CanvasMapControl'的类型初始值设定项引发异常。'

     

内部异常

     

ArgumentException:参数不正确。 createDefaultValueCallback

我尝试过的事情:

  • 将DependencyProperty更改为int并强制转换
  • 为PropertyMetadata
  • 传递null
  • 将null设置为PropertyMetadata的默认值
  • 传递函数而不是默认值的值

无论我尝试什么,在启动时它总是抛出针对ActiveInteractionMode的SetValue行的上述异常。我错过了什么让这不起作用?

更新:InteractionModeInternal是一个简单的私有属性,可用于在处理过程中临时更改ActiveInteractionMode

private InteractionMode _interactionModeInternal;

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

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

我目前没有逻辑驱动它,除了它将一个字符串记录到控制台。

更新2 :进一步调试后,我发现启动时崩溃的原因是我在XAML中绑定了这些属性的值。在注释掉这些绑定之后,应用程序能够启动,但是然后尝试访问Get或Set for ActiveInteractionMode将导致与createDefaultValueCallback发生同样的错误。

2 个答案:

答案 0 :(得分:0)

我已经通过提供段代码编写了一个示例。我试图重现你的问题。但是我的项目没有出现这个问题。我已将code sample上传到github。请检查。以下代码是自定义控件,您可以比较以下代码以查明项目中是否存在问题。

 public enum InteractionMode
 {
  None,
  Initiative,
  Passive
 }

 public sealed partial class CustomUserControl : UserControl
 {
     public CustomUserControl()
     {
         this.InitializeComponent();
     }

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

 public static readonly DependencyProperty ActiveInteractionModeProperty =
     DependencyProperty.Register("ActiveInteractionMode", typeof(InteractionMode), typeof(CustomUserControl), new PropertyMetadata(InteractionMode.None, CanvasMapControl_InteractionModeChanged));

 private static void CanvasMapControl_InteractionModeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
 {
     if (args.OldValue != args.NewValue)
     {
         ((CustomUserControl)obj).InteractionModeInternal = (InteractionMode)args.NewValue;
     }
 }

 private InteractionMode _interactionModeInternal;

 private InteractionMode InteractionModeInternal
 {
     get
     {
         return _interactionModeInternal;
     }

     set
     {
         _interactionModeInternal = value;
         OnInteractionModeInternalChanged(value);
     }
 }

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

答案 1 :(得分:0)

如果将来遇到这种情况,我会将此作为答案发布,因为它确定了我遇到的具体问题。我不知道我做错了什么,或者这是UWP或我的手机上的错误,但我不再有这个代码,所以无法进一步调试。

  

更新2:经过进一步调试后,我发现了崩溃的原因   在启动时发生的是我将值绑定到这些属性中   XAML。在评论出这些绑定后,应用程序能够   启动,但然后尝试访问Get或Set for   ActiveInteractionMode会导致同样的错误   发生createDefaultValueCallback。