Is it correct to call a method inside a Property Setter?

Irs*_*had 0 c# wpf

In a WPF application I'm working on I came across following code snippet;

public int SomeInteger
{
    get { return _someInteger; }
    set
    {
        _someInteger= value;
        OnPropertyChanged("SomeInteger");
        SomeIntegerChanged();
    }
}
Run Code Online (Sandbox Code Playgroud)

As you can see, inside the property setter a method is called. Is this approach correct or is there any better way to do in MVVM pattern WPF?

As above code snippet is a sample, actually the property setter might load a DataTable when the property changes which might be time consuming. I tried to make the application asynchronous using async and await but above scenarios causes problems since async and await doesn't have asynchronous property concept.

I also got across a way to bind a event to INotifyPropertyChanged and by checking which property triggers the event and call the method. But looking for any other alternatives.

Ste*_*ary 5

这种方法是正确的还是在MVVM模式WPF中有更好的方法呢?

不幸的是,这是跨所有C#的通用和公认的MVVM模式。我说“不幸”是因为这种模式确实遇到了问题。它基于事件-本身就存在问题-并且还导致立即更新,这在许多情况下是不希望的。

例如,有时候希望具有两个相互依赖的属性-当用户更改一个属性时,另一个属性相对于它更改-这会引起问题,因为无法将“用户更改”与“代码更改”区分开”。

再举一个例子,有时您可能会得到一整套依赖属性树,并且所有更改都需要花费一些时间才能传播并停止相互干扰。

更现代的MVVM方法(如具有单向数据流原子更新的单个事实真源)(由Redux推广)避免了上述问题。有一个C#Redux实现;我不知道使用起来有多容易。在我自己的CalculatedProperties库中,我建立了自己的“无效队列”来解决此问题,该队列将所有更新推迟到计算出整个系统的新稳态之后。PropertyChanged

我试图使用async和await使应用程序异步,但是由于async和await没有异步属性概念,因此上述情况会导致问题。

真正。特别是,您不能“异步获取”属性。这在MVVM世界中是有意义的。当WPF更新屏幕时,它要求您的VM提供数据值时,您的VM不会说“保持,请稍后”。WPF需要知道该值现在,因此它可以更新屏幕现在

连接异步事件处理程序是在值更改时启动异步工作的一种方法。这种方法很好。通常,SomeIntegerChanged隐含的存在意味着存在专门针对该属性的“已更改”事件,因此加入PropertyChanged字符串比较可能是更困难的方法。

有关MVVM的异步属性的更多信息,请参阅有关该主题的文章