通知viewmodel的所有属性已更改为null或字符串为empty [英] Notifying all properties of the viewmodel has changed with null or string empty

查看:128
本文介绍了通知viewmodel的所有属性已更改为null或字符串为empty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自开发WPF解决方案,在那里更新视图模型的所有属性非常简单:

I'm coming from developing WPF solutions where to update all properties of the viewmodel was as simple as:

OnPropertyChanged(String.Empty);

在通用Windows平台方案中,我只是用相同的方法来更新/刷新属性.在大多数情况下,此方法工作正常,但有时会引发如下错误:

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错误HRESULT E_FAIL已从对COM组件的调用返回.在System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender,PropertyChangedEventArgs e)在GeekyTool.Base.BindableBase.OnPropertyChanged(String propertyName)在Pooo.set_Root(UserRoot value)在Booo.d__26.MoveNext()---堆栈跟踪结束从先前引发异常的位置开始-在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在GeekyTool.Base.PageBase.d__1.MoveNext() ---从之前引发异常的位置开始的堆栈跟踪---在System.Runtime.CompilerServices.AsyncMethodBuilderCore.<> c.b__6_0(对象状态)在System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()

具有INotifyPropertyChanged接口实现的OnPropertyChanged方法如下所示:

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;
    }
}

您可以探索mvvm库,但INotifyPropertyChanged实现上没有什么不同.

You can explore the mvvm library, but nothing different on the INotifyPropertyChanged implementation.

Github上的GeekyTool库

推荐答案

感谢所有的回答,我正在尝试修复一个错误,提示它不存在.

Thank's for all the answers, I was trying to fix an error that it isn't was there.

在OnPropertyChanged(string.Empty)方法中引发错误,因为它带有来自上一页的同步上下文问题.

In the OnPropertyChanged(string.Empty) method raise the error because it comes with an sync context issue from the page before.

当您在两个页面之间快速导航并且在尚未完成的OnNavigatedTo方法中有一些异步调用时,会发生这种情况.等待异步方法,但是在此页面中,用户没有等待该操作完成.

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.

只知道不需要应用@PedroLamas修复程序.确保在页面上完成所有异步调用之前,此操作已完成.

Just to know that no need to apply @PedroLamas fix. Ensuring on the page before that all async calls are finished it's done.

这篇关于通知viewmodel的所有属性已更改为null或字符串为empty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆