Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在INotifyPropertyChanged上引发PropertyChanged事件时出错_C#_.net_Wpf_Silverlight_Windows Phone 7 - Fatal编程技术网

C# 在INotifyPropertyChanged上引发PropertyChanged事件时出错

C# 在INotifyPropertyChanged上引发PropertyChanged事件时出错,c#,.net,wpf,silverlight,windows-phone-7,C#,.net,Wpf,Silverlight,Windows Phone 7,我在物理电话上运行此代码时出错,但在模拟器上没有: System.ArgumentException“参数不正确” 当我使用以下代码时。这是我创建的自定义类型,允许我轻松创建可以绑定到视图的类型 在此行引发异常: this.PropertyChanged(this, new PropertyChangedEventArgs("Value")); 在我将其变为通用之前,它工作得非常好: public class BindableType<T> : INotifyPropertyCha

我在物理电话上运行此代码时出错,但在模拟器上没有:

System.ArgumentException“参数不正确”

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

在此行引发异常:

this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
在我将其变为通用之前,它工作得非常好:

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; }
    }
}
公共类BindableType:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私人T_值;
私人T_先前值;
公共价值
{
收到
{
返回_值;
}
设置
{
_previousValue=_值;
_价值=价值;
if(this.PropertyChanged!=null)
{
this.PropertyChanged(这是新的PropertyChangedEventArgs(“值”));
}
}
}
公共优先价值
{
获取{return\u previousValue;}
}
}
以下是绑定代码:

以下是堆栈跟踪:

在MS.Internal.XcpImports.CheckHResult(UInt32小时)处 MS.Internal.XcpImports.SetValue(IManagedPeerBase对象, DependencyProperty属性,双d)位于 MS.Internal.XcpImports.SetValue(IManagedPeerBase-doh, DependencyProperty属性,对象对象对象)位于 System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp,对象值)在 System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty 属性,EffectiveValueEntry和newEntry,对象newValue)位于 System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty 属性、有效值条目oldEntry、有效值条目和新条目, 值(操作)在 System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)在System.Windows.Data.BindingExpression.RefreshExpression()上 位于System.Windows.Data.BindingExpression.SendDataToTarget()的 System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener 发件人,PropertyPathChangedEventArgs)位于 System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep 来源)在 System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener 来源)在 System.Windows.CLRPropertyListener.SourcePropertyChanged(对象 发件人,PropertyChangedEventArgs)位于 System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(对象 发件人,PropertyChangedEventArgs)位于 RoadCast.Model.BindableType
1.将_值(双值)设置为
RoadCast.Default.locationHelper\u PositionChangedMinor(对象发送方,
地理位置更改方向标记
1个参数) RoadCast.Core.LocationHelper.watcher\u位置已更改(对象发送器, 地理位置更改标记'1 e)位于 系统.设备.位置.地理坐标切换器.c_显示器Class1.b_0(对象 _)在System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi、对象对象、BindingFlags invokeAttr、绑定器、对象 参数,CultureInfo区域性,布尔值isBinderDefault,程序集 调用方、布尔验证访问、堆栈爬网标记和堆栈标记)位于 System.Reflection.RuntimeMethodInfo.InternalInvoke(对象obj, BindingFlags invokeAttr,Binder Binder,对象[]参数, 文化信息文化、堆栈爬网标记和堆栈标记)位于 System.Reflection.MethodBase.Invoke(对象obj,对象[]参数)
位于System.Delegate.DynamicInvokeOne(对象[]args)处 System.MulticastDelegate.DynamicInvokeImpl(对象[]args)位于 System.Delegate.DynamicInvoke(对象[]args)位于 System.Windows.Threading.DispatcherOperation.Invoke()位于 System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority 优先级)在System.Windows.Threading.Dispatcher.OnInvoke(对象 在System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)在 System.Windows.Hosting.DelegateWrapper.InternalInvoke(对象[]参数)
位于System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr 幻影,Int32 nParamCount,ScriptParam[]pParams,ScriptParam& 预设)


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

您需要初始化属性更改为:

  public event PropertyChangedEventHandler PropertyChanged = delegate { };

请发布异常的堆栈跟踪。我会尝试将其称为与Value不同的名称,但这只是一个范围。似乎工作正常
其中
MyProperty
的类型为
BindableType
@ElleryFamilia-只是一条注释-但您不检查value=value(即属性实际上没有改变)。您也没有通知PreviousValue已更改。各位,我更新问题。请注意,只有在设备上运行代码时才会发生异常,而不是在模拟器上运行代码。@ElleryFamilia这里有一篇大文章(搜索避免检查空事件处理程序)谈到了这是一个多么糟糕的主意。