Синхронизация свойств MVVM между моделями представленийC#

Место общения программистов C#
Ответить
Anonymous
 Синхронизация свойств MVVM между моделями представлений

Сообщение Anonymous »


Imagine this situation: There are viewmodels ViewModelA and ViewModelB. Both models are a datasource (source of truth) for a value, we can call it as "selected". There is a "Selected" property on both viewmodels. The "Selected property" is notifiable property (it invokes OnPropertyChanged event). When the ViewModelA updates the "selected" value then the ViewModelB is notified about this change and vice versa.

My current solution: Both viewmodels share their references with eachother (ViewModelA have a reference to ViewModelB an vice versa). When the Selected property is set with a new value (OnPropertyChanged is ivoked) then this property is set also one the otherside.

The catch: It's obvious what's wrong with my current solution: It creates infinite loop cycle of setting properties (ViewModelA sets value -> ViewModelA notifies ViewModelB -> ViewModelB sets value -> ViewModelB notifies ViewModelA -> ViewModelA sets vallue -> ...). My solution only works because when the originator of the change is notified about the change (that it made originally) the SetProperty method doesn't invoke OnPropertyChanged because the values are the same. But there is a risk of an infinitive loop in case the comparison of an old and the new one evaluates (from some reason) incorrectly.

Question: How to safely sync values between two viewmodels?


Источник: https://stackoverflow.com/questions/781 ... viewmodels
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»