c# - 当我需要 "refresh"或 "update" View 时,为我的 ViewModel 中的每个属性调用 RaisePropertyChanged() 是不好的做法吗?

标签 c# wpf performance mvvm

我目前使用反射来获取特定类型或访问级别的类中的所有属性名称。然后,我通过 RaisePropertyChanged() 运行它们以实质上“更新”整个 View 。

我执行此操作的一个实例是在启动时,当程序启动并且实例化 ViewModel 时,它将运行它以确保 View 显示模型中的所有正确数据。

这样做有什么问题吗?

代码,如果你们想要的话:

    private void InitializeViewModel()
    {
        foreach (string name in MiscMethods.GetPropertyNames(this))
        {
            RaisePropertyChanged(name);
        }
    }

    public static IEnumerable<string> GetPropertyNames(Object yourClass)
    {
        foreach (PropertyInfo property in GetProperties(yourClass))
        {
            yield return property.Name;
        }
    }

    //Uses Reflection to return all properties in a class
    private static IEnumerable<PropertyInfo> GetProperties(Object theObject)
    {
        return theObject.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance);
    }

最佳答案

带空字符串的上升事件

RaisePropertyChanged("");

这将触发对所有 属性的更新。

关于c# - 当我需要 "refresh"或 "update" View 时,为我的 ViewModel 中的每个属性调用 RaisePropertyChanged() 是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31719215/

相关文章:

c# - WPF 中 UIElement 的核心属性是什么?

wpf - HttpClient 上传进度条

python - 为什么使用切片复制列表[:] faster than using the obvious way?

c# - 在代码隐藏中创建表行

c# - 使用 C# 获取 SQL Server 列的数据类型

c# - Mage.exe 不为 4.5 生成兼容的框架

java - 使用 int 然后递增它比同时做两者慢吗?

c# - 返回的存储库模式和数据类型

wpf - 如何在 Model-View-Viewmodel 架构中从模型方法的中间干净地获取用户输入?

css - CSS 的速度