繁体   English   中英

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

[英]Error when I raised the PropertyChanged event on INotifyPropertyChanged

在物理电话上而不是在模拟器上运行此代码时遇到错误:

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.SetValue(IManagedPeerBase对象,DependencyProperty属性,Double d)在MS.Internal.XcpImports.SetValue(IManagedPeerBase obj,DependencyProperty属性,Double d)在MS.Internal.XcpImports.SetValue(IManagedPeerBase doh,DependencyProperty属性,对象obj) System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty属性,EffectiveValueEntry&newEntry,Object newValue)处的Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp,Object value)System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty属性,EffectiveValueEntryValueryryEntry,newEntry,EffectiveValueEntry& System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)上的System.Windows.Data.BindingExpression.RefreshExpression()上的操作)
在System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener发送者,PropertyPathChangedEventArgs args)在System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep源)在System.Windows.PropertyAccessPathStep。 System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(对象发送者,PropertyChangedEventArgs args)位于System.Windows.CLRPropertyListener.SourcePropertyChanged(对象发送者,PropertyChangedEventArgs args),位于RoadCast.Model.BindableType 1.set_Value(Double value) at RoadCast.Default.locationHelper_PositionChangedMinor(Object sender, GeoPositionChangedEventArgs .RoadCast.Core.LocationHelper.watcher_PositionChanged(对象发件人,GeoPositionChangedEventArgs`1 e)在System.Device.Location.GeoCoordinateWatcher。<> c_ DisplayClass1_b处的Default.locationHelper_PositionChangedMinor(对象发送1.set_Value(Double value) at RoadCast.Default.locationHelper_PositionChangedMinor(Object sender, GeoPositionChangedEventArgs 1 args)在对象_0(Object System.Reflection.RuntimeMethodInfo.InternalIn 在System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,BindingFlags invokeAttr,Binder活页夹,voke(RuntimeMethodInfo rtmi,Object obj,BindingFlags invokeAttr,Binder活页夹,对象参数,CultureInfo文化,Boolean isBinderDefault,程序集调用者,Boolean verifyAccess,StackCrawlMark&stackMark)。 Object []参数,CultureInfo文化,System.Reflection.MethodBase.Invoke处的StackCrawlMark&stackMark)(Object obj,Object []参数)
在系统上System.Delegate.DynamicInvoke(Object [] args)在System.Windows.Threading.DispatcherOperation.Invoke()在System.Delegate.DynamicInvokeOne(Object [] args)在System.Delegate.DynamicInvokeImpl(Object [] args)在System.Windows.Threading.DispatcherOperation.Invoke() System.Windows.Threading.Dispatcher.OnInvoke(对象上下文)(位于System.Windows.Hosting.CallbackCookie.Invoke(Object [] args),位于System.Windows.Hosting.DelegateWrapper,位于.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority优先级) .InternalInvoke(Object [] args)
在System.Windows.RuntimeHost.ManagedHost.InvokeDelegate处(IntPtr pHandle,Int32 nParamCount,ScriptParam [] pParams,ScriptParam&pResult)

更新:将属性重命名为“ InternalValue”为我解决了此问题。

您需要将PropertyChanged初始化为:

  public event PropertyChangedEventHandler PropertyChanged = delegate { };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM