1

I'm getting an error when I run this code on the physical phone, but not on the emulator:

System.ArgumentException "The parameter is incorrect"

when I use the following code. This is a custom type I created to allow me to easily create types that can Bind to the View.

The exception is thrown on this line:

this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));

This worked perfectly fine until I made it Generic:

public class BindableType<T> : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    private T _value;
    private T _previousValue;

    public T Value
    {
        get
        {
            return _value;
        }
        set
        {
            _previousValue = _value;
            _value = value;
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }
    }

    public T PreviousValue
    {
        get { return _previousValue; }
    }
}

Here is the binding code:

Here is the stack trace:

at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, Double d) at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj) at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value) at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue) at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation) at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp) at System.Windows.Data.BindingExpression.RefreshExpression()
at System.Windows.Data.BindingExpression.SendDataToTarget() at System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener sender, PropertyPathChangedEventArgs args) at System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep source) at System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source) at System.Windows.CLRPropertyListener.SourcePropertyChanged(Object sender, PropertyChangedEventArgs args) at System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(Object sender, PropertyChangedEventArgs args) at RoadCast.Model.BindableType1.set_Value(Double value) at RoadCast.Default.locationHelper_PositionChangedMinor(Object sender, GeoPositionChangedEventArgs1 args) at RoadCast.Core.LocationHelper.watcher_PositionChanged(Object sender, GeoPositionChangedEventArgs`1 e) at System.Device.Location.GeoCoordinateWatcher.<>c_DisplayClass1.b_0(Object _) at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args) at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) at System.Delegate.DynamicInvoke(Object[] args) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) at System.Windows.Threading.Dispatcher.OnInvoke(Object context) at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

UPDATE: Renaming the Property to "InternalValue" fixed this for me.

Ellery Familia
  • 530
  • 5
  • 20
  • Please, post the stack trace of the exception. – Pavel Gatilov Dec 23 '11 at 19:41
  • 1
    I would try calling it something different than Value but that is just a reach. – paparazzo Dec 23 '11 at 20:17
  • Seems to work fine. `` where `MyProperty` is of type `BindableType` – Tomislav Markovski Dec 23 '11 at 22:07
  • 2
    @ElleryFamilia - just a comment - but you do not check if value = value (i.e. the property isn't actually changing). You are also not Notifying that PreviousValue has changed. – William Melani Dec 23 '11 at 22:23
  • Folks, I update the question. Please note that the Exception only happens when running the code on the device, but not on the emulator. – Ellery Familia Dec 24 '11 at 02:58
  • @BalamBalam That was it! I renamed the Property to "InternalValue" and it works fine now. That's really strange behavior.. especially since it only happened once I made the type generic. – Ellery Familia Dec 24 '11 at 03:17
  • @willmel Thanks for your comment. I actually don't want a notification when PreviousValue changes, but I should certainly check if value = value. – Ellery Familia Dec 24 '11 at 03:19
  • UPDATE: While changing the name of the Property stopped the runtime exception, the PropertyChanged now had a null value. Mark W's answer below resolved this issue completely. – Ellery Familia Dec 24 '11 at 04:45

1 Answers1

0

you need to initialize your PropertyChanged to this:

  public event PropertyChangedEventHandler PropertyChanged = delegate { };
Mark W
  • 3,879
  • 2
  • 37
  • 53
  • @ElleryFamilia There's a big post here http://stackoverflow.com/questions/9033/hidden-features-of-c (search for Avoid checking for null event handlers) that talks about how this is a bad idea. – William Melani Dec 24 '11 at 05:02