.net - WPF MVVM Correct way to fire event on view from ViewModel -
In my WPF application I have 2 windows (both windows have their own view modell):
- < Li>
-
The list of the dialog window users (AddWordViewModel Binding) that allows
The main window of the application that displays the list with the bunch of words (bound to MainViewModel)
to force the mainViewModel article (this is one of the collection service sections populated) The property of the list is bound to ListBox of the main window
AddWordViewModel SaveWordCommand is to add the word dialog button, this job is to take the text entered by the user and pass it in the service class.
The user clicks the Save button, I need to inform the main visual model to reload the article from the service.
My idea was to expose the public order in MainViewModel and execut it with AddWordViewModel
is the right way to implement it?
Thank you!
A great way to solve this type of problem is actually a centralized class (simplicity For, we say that this is a singleton and is facing the potential anger of single singles people) who is responsible for moving from one object to another. Your class may look like use with the name:; WordAddedEvent & gt; ()
public class MainViewModel {public MainViewModel () {WordAddedEvent event = EventAggregator.Instance.GetEvent & LT; Event.Subscribe (WordAdded); } Protected Virtual Zero WordAdded (Object This WordAddedEventArgs e) {// handle event}} Public class AddWordViewModel {// Command Public ExecuteAddWord (string word) {WordAddedEvent event = EventAggregator.Instance.GetEvent & LT; WordAddedEvent & gt; (); Event.Publish (this, new WordAddedEventArgs (word)); }} The advantage of this method is that you can expand many ways and many choices to make words interesting, which are interested in the words that have been added And this is not the pairing between the two so that you can add them and require them to be removed.
If you want to avoid singleton (and I recommend that you do this for testing), then it is possible in dependency injection, though it is actually another other problem is. Well, the final idea is that I read your question again, that you already have a Word Service class that manages the recovery and storage of Word objects. There is no reason that when new words are added after service, the service can not be made responsible since the Vyomodal has already been linked to both. Although I still suggest that the event ajugator is a more convenient and better solution, but can be applied here
public class WordService {public event event handler & lt; WordAddedEventArgs & gt; WordAdded; Public listing & lt; String & gt; GetAllWords () {// Return word} Public Zero Saveword (string word) {// Save word if (WordAdded! = Null) WordAdded (this, new WordAddedEventArgs (word)); // Note that this is the way you lost in terms of where the word actually came from //, probably does not matter}, but may be}} public class MainViewModel {public MainViewModel () / // Services WordAdded event }} Add eventhandler Whatever you want to do, you want to present the pairing between the view modules, which you can create with a command call on the other with a view modal It is, for the extension of your application. Will be restricted by a lot (what if a second view model is interested in new words, is it still an additional responsibility to tell one?)
Comments
Post a Comment