secile / UsbCamera

C# source code for using usb camera and web camera in WinForms/WPF. With only single CSharp source code. No external library required.
MIT License
179 stars 55 forks source link

wpf example : RaiseAndSetIfChanged replace OnPropertyChanged #37

Open yangjieshao opened 3 months ago

yangjieshao commented 3 months ago

wpf example RaiseAndSetIfChanged replace OnPropertyChanged

secile commented 3 months ago

Thank you for your contribution.

I confirmed that your code works correctly. But there is a difference between current code and your code.

[Current code] called OnPropertyChanged on every camera.PreviewCaptured.

[Your code] called OnPropertyChanged first time only in camera.PreviewCaptured call.

This is because camera.Preview Capture passes a single-instance bitmap created by the library. Therefore, the same instance is passed in every camera.PreviewCapture call.

It is true that the application is updating the screen without having to call OnPropertyChanged every time, but I'm not sure which should be whether the current code or your code.

Could you tell me your opinion.

yangjieshao commented 3 months ago
if (!ReferenceEquals(backingField, newValue))

只有当新的值和原来的值不是同一个对象的时候 才去调用 OnPropertyChanged (Only call OnPropertyChanged when the new value is not the same object as the original value)

这样可以避免不必要的UI更新 (This can avoid unnecessary UI updates)

secile commented 3 months ago

Thank you for your response.

You said that your code can avoid unnecessary call of INotifyPropertyChanged. But is it really unnecessary?

What worries me is that the reference of the Preview image hasn't changed, but the Preview image itself has changed.

This is a description of the INotifyPropertyChanged Interface

Notifies clients that a property value has changed.

According from the article, I think that I need to call INotifyPropertyChanged.

Off cource I know that your code works fine, but I want to choice the selection more safe.