我在INotifyPropertyChanged上引发PropertyChanged事件时出错

时间:2011-12-23 19:14:36

标签: c# .net wpf silverlight windows-phone-7

我在物理手机上运行此代码时遇到错误,但在模拟器上没有:

  

System.ArgumentException“参数不正确”

当我使用以下代码时。这是我创建的自定义类型,允许我轻松创建可以绑定到视图的类型。

此行引发异常:

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

这个工作完全正常,直到我制作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; }
    }
}

这是绑定代码:

这是堆栈跟踪:

  

在MS.Internal.XcpImports.CheckHResult(UInt32 hr)at   MS.Internal.XcpImports.SetValue(IManagedPeerBase obj,   DependencyProperty属性,Double d)at   MS.Internal.XcpImports.SetValue(IManagedPeerBase doh,   DependencyProperty属性,Object obj)at   System.Windows.DependencyObject.SetObjectValueToCore(的DependencyProperty   dp,对象值)at   System.Windows.DependencyObject.SetEffectiveValue(的DependencyProperty   property,EffectiveValueEntry&amp; newEntry,Object newValue)at   System.Windows.DependencyObject.UpdateEffectiveValue(的DependencyProperty   property,EffectiveValueEntry oldEntry,EffectiveValueEntry&amp;新条目,   ValueOperation操作)at   System.Windows.DependencyObject.RefreshExpression(的DependencyProperty   dp)at System.Windows.Data.BindingExpression.RefreshExpression()
  在System.Windows.Data.BindingExpression.SendDataToTarget()at   System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener   发件人,PropertyPathChangedEventArgs args)at   System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep   来源)   System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener   来源)   System.Windows.CLRPropertyListener.SourcePropertyChanged(对象   发件人,PropertyChangedEventArgs args)at   System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(对象   发件人,PropertyChangedEventArgs args)at   RoadCast.Model.BindableType 1.set_Value(Double value) at RoadCast.Default.locationHelper_PositionChangedMinor(Object sender, GeoPositionChangedEventArgs 1 args)at   RoadCast.Core.LocationHelper.watcher_PositionChanged(对象发送者,   GeoPositionChangedEventArgs`1 e)at   System.Device.Location.GeoCoordinateWatcher&LT;&GT; C_ DisplayClass1.b _0(对象   _)at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo)   rtmi,Object obj,BindingFlags invokeAttr,Binder binder,Object   参数,CultureInfo文化,布尔isBinderDefault,汇编   调用者,布尔验证访问,StackCrawlMark&amp; stackMark)at   System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,   BindingFlags invokeAttr,Binder binder,Object []参数,   CultureInfo culture,StackCrawlMark&amp; stackMark)at   System.Reflection.MethodBase.Invoke(Object obj,Object []参数)
  在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   System.Windows.Threading.Dispatcher.OnInvoke(Object。的优先级)   System.Windows.Hosting.CallbackCookie.Invoke(Object []上下文)   args)at   System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object [] args)
  在System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr   pHandle,Int32 nParamCount,ScriptParam [] pParams,ScriptParam&amp;   pResult)

更新:将属性重命名为“InternalValue”为我修复此问题。

1 个答案:

答案 0 :(得分:0)

您需要将PropertyChanged初始化为:

  public event PropertyChangedEventHandler PropertyChanged = delegate { };
相关问题