How can a ViewModel know when data in a service is being updated?

In my application, I have several ViewModels that have a separate service (repository, DAO, whatever), let me call it the WidgetService introduced in them.

Let's say that one of these ViewModels is a list of all user widgets. Another may be a ViewModel for editing / creating one of these widgets.

The user can view the list of widgets WidgetListView supporting WidgetListViewModel and click a button to add a new widget. To create this new widget, CreateWidgetViewModel is updated and inserted into the DataContext of some UserControl / Window, thus, thanks to the magic of DataTemplates displaying CreateWidgetViewModel in CreateWidgetView . In addition, the innovation of CreateWidgetViewModel does not necessarily occur as part of the WidgetListViewModel .

When the WidgetListViewModel instance of WidgetService was added . CreateWidgetViewModel This WidgetService tag has been added .

Now, when the user clicks on save in CreateWidgetView , the Save method in WidgetService will be called and the widget will be saved. Now WidgetListViewModel needs to be notified about the appearance of a new widget!

A lengthy build leads to this question: how can I let WidgetListViewModel know that it needs to display a new widget?

Video, Microsoft , , ViewModel . , , , viewmodel GC'd, GC'd. IDisposable ViewModel. , / Dispose, ViewModel DataTemplates?

- - ?

, , MVVM . , MVVM- Crack.Net.

+3
5

, , , , , MVVM.

, , . , , , ViewModel. - .

, , GC'd, .

+1

(http://www.codeplex.com/CompositeWPF) EventAggregator, , . , .

+1

( /) . , EventAggregator, . WidgetListViewModel " " CreateWidgetViewModel. WidgetListViewModel IDisposable (), , Finalize. , , .

- , ModelView, , M-MV-VM-V. , . , , MV, .

0

:

  • ObservableCollection - , , , - "" , "" MVVM , , .

  • , , MVVM.

  • ( ), , , WPF , , ( - , ).

  • Update periodically, you have a ViewModel class that updates the list every X seconds, this is the only way to get updates without any notification mechanism that you have to manage.

0
source

I agree that Cameron uses the WeakEvent template. I created a base class for ViewModel (in my example, I use the name PresentationModel) that supports the WeakEvent template.

You may find my sample project useful: http://www.codeplex.com/CompositeExtensions

Jbe

0
source

Source: https://habr.com/ru/post/1704760/


All Articles