I'm coming from developing WPF solutions where to update all properties of the viewmodel was as simple as:
OnPropertyChanged(String.Empty);
In the Universal Windows Platform scenario, I just have the same method to update/refresh the properties. This works fine in most of cases but sometimes it raise an error something like this:
COMException Error HRESULT E_FAIL has been returned from a call to a COM component. at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e) at GeekyTool.Base.BindableBase.OnPropertyChanged(String propertyName) at Pooo.set_Root(UserRoot value) at Booo.d__26.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at GeekyTool.Base.PageBase.d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.b__6_0(Object state) at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
The OnPropertyChanged method with an INotifyPropertyChanged interface implementation look like this:
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;
}
}
You can explore the mvvm library, but nothing different on the INotifyPropertyChanged implementation.
I see on the stack trace that there's some async code, so I'd suggest only invoking OnPropertyChanged(String.Empty) with the Dispatcher, like so:
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OnPropertyChanged(string.Empty);
});
Thank's for all the answers, I was trying to fix an error that it isn't was there.
In the OnPropertyChanged(string.Empty) method raise the error because it comes with an sync context issue from the page before.
It happens when you are navigating between two page very fast and has some async calls in the OnNavigatedTo method where they aren't finished yet. The async method are awaited, but in this page was not handled that the user wait until this is finished.
Just to know that no need to apply @PedroLamas fix. Ensuring on the page before that all async calls are finished it's done.
CallerMemeberName pulls calling member name if you pass in nothing (or null) that isn’t the same string.empty
I’d fix that first.
public bool IsValid
{
get { return isValid; }
set
{
if (isValid == value)
{
return;
}
isValid = value;
OnPropertyChanged();
}
}
This should work. Often where I can’t use ReactiveObject or ObservableObject I tend to use this.
来源:https://stackoverflow.com/questions/43049019/notifying-all-properties-of-the-viewmodel-has-changed-with-null-or-string-empty