C# 通知viewmodel的所有属性已更改为null或字符串为空
我来自开发WPF解决方案,其中更新viewmodel的所有属性非常简单:C# 通知viewmodel的所有属性已更改为null或字符串为空,c#,mvvm,uwp,inotifypropertychanged,C#,Mvvm,Uwp,Inotifypropertychanged,我来自开发WPF解决方案,其中更新viewmodel的所有属性非常简单: OnPropertyChanged(String.Empty); 在通用Windows平台场景中,我只使用相同的方法更新/刷新属性。这在大多数情况下都可以正常工作,但有时会引发如下错误: public abstract class BindableBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyCha
OnPropertyChanged(String.Empty);
在通用Windows平台场景中,我只使用相同的方法更新/刷新属性。这在大多数情况下都可以正常工作,但有时会引发如下错误:
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
public virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public virtual bool Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
}
对COM组件的调用返回了COMException错误HRESULT E_FAIL。在GeekyTool.Base.BindableBase.OnPropertyChanged(String propertyName)的System.ComponentModel.PropertyChangedEventHandler.Invoke(对象发送方,PropertyChangedEventArgs e)中,在Pooo.set_根(UserRoot值)中,在Booo.d_u26.MoveNext()中---从引发异常的上一个位置开始的堆栈结束跟踪---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForOnSuccess(任务任务任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务)在GeekyTool.Base.PageBase.d_u1.MoveNext()---在System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()上的System.Runtime.CompilerServices.AsynchmethodBuilderCore.c.b_u6_0(对象状态)上引发异常的上一个位置的堆栈结束跟踪
带有INotifyPropertyChanged
接口实现的OnPropertyChanged
方法如下所示:
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
public virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public virtual bool Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
}
公共抽象类BindableBase:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
公共虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
公共虚拟布尔集合(参考T存储,T值,[CallerMemberName]字符串propertyName=null)
{
if(object.Equals(存储,值))
返回false;
储存=价值;
OnPropertyChanged(propertyName);
返回true;
}
}
您可以浏览mvvm库,但在INotifyPropertyChanged实现上没有什么不同
我在堆栈跟踪中看到一些异步代码,因此我建议只使用
调度程序调用OnPropertyChanged(String.Empty)
,如下所示:
Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>
{
OnPropertyChanged(string.Empty);
});
CallerMeberName如果未传入任何与字符串不同的内容(或null),则会提取调用成员名称。empty
我会先修好的
public bool IsValid
{
get { return isValid; }
set
{
if (isValid == value)
{
return;
}
isValid = value;
OnPropertyChanged();
}
}
这应该行得通。通常,当我不能使用ReactiveObject或observeObject时,我倾向于使用它。感谢所有的答案,我试图修复一个错误,它不在那里
在OnPropertyChanged(string.Empty)方法中,引发错误,因为它与前面页面的同步上下文问题一起出现
当您在两个页面之间快速导航,并且OnNavigatedTo方法中的一些异步调用尚未完成时,就会发生这种情况。正在等待异步方法,但在此页面中未处理该方法,用户将等待该方法完成
只知道不需要应用@PedroLamas修复程序。在页面上确保所有异步调用完成之前,它已完成 我很惊讶这是否对UWP有效。代码中没有特殊的内容来处理属性名String.Empty
。你们有完整的复印件吗@MattLacey,用string.empty
引发INotifyPropertyChanged.PropertyChanged
事件是AFAIK的一个“老”技巧,在UWP中可以很好地强制所有(经典或编译的)绑定刷新!如果使用x:bind,则可以从页面的代码隐藏中调用“this.Bindings.Update()”,以强制更新该页面上的所有绑定;是在setter中完成的,所以我可以做的是一个异步操作,而无需等待它。对吗?谢谢正确,您不需要等待调度程序
呼叫,您在这里需要什么并不重要!是的,但正如@PedroLamas所说,用string.Empty
引发INotifyPropertyChanged.PropertyChanged
事件是一个“老”把戏,可以强制所有绑定更新/刷新。这就是我正在做的,一次更新所有内容。