SwiftUI – How to pass EnvironmentObject into View Model?

You can do it like this: struct YourView: View { @EnvironmentObject var settings: UserSettings @ObservedObject var viewModel = YourViewModel() var body: some View { VStack { Text(“Hello”) } .onAppear { self.viewModel.setup(self.settings) } } } For the ViewModel: class YourViewModel: ObservableObject { var settings: UserSettings? func setup(_ settings: UserSettings) { self.settings = settings } }

Close Window from ViewModel [duplicate]

You can pass the window to your ViewModel using the CommandParameter. See my Example below. I’ve implemented an CloseWindow Method which takes a Windows as parameter and closes it. The window is passed to the ViewModel via CommandParameter. Note that you need to define an x:Name for the window which should be close. In my … Read more

What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?

Intro In MVVM the usual practice is to have the Views find their ViewModels by resolving them from a dependency injection (DI) container. This happens automatically when the container is asked to provide (resolve) an instance of the View class. The container injects the ViewModel into the View by calling a constructor of the View … Read more

How to handle dependency injection in a WPF/MVVM application

I have been using Ninject, and found that it’s a pleasure to work with. Everything is set up in code, the syntax is fairly straightforward and it has a good documentation (and plenty of answers on SO). So basically it goes like this: Create the view model, and take the IStorage interface as constructor parameter: … Read more

MVVM in WPF – How to alert ViewModel of changes in Model… or should I?

If you want your Models to alert the ViewModels of changes, they should implement INotifyPropertyChanged, and the ViewModels should subscribe to receive PropertyChange notifications. Your code might look something like this: // Attach EventHandler PlayerModel.PropertyChanged += PlayerModel_PropertyChanged; … // When property gets changed in the Model, raise the PropertyChanged // event of the ViewModel copy … Read more

Is there a proper way of resetting a component’s initial data in vuejs?

extract the initial data into a function outside of the component use that function to set the initial data in the component re-use that function to reset the state when needed. // outside of the component: function initialState (){ return { modalBodyDisplay: ‘getUserInput’, submitButtonText: ‘Lookup’, addressToConfirm: null, bestViewedByTheseBounds: null, location:{ name: null, address: null, position: … Read more

Pushing read-only GUI properties back into ViewModel

Yes, I’ve done this in the past with the ActualWidth and ActualHeight properties, both of which are read-only. I created an attached behavior that has ObservedWidth and ObservedHeight attached properties. It also has an Observe property that is used to do the initial hook-up. Usage looks like this: <UserControl … SizeObserver.Observe=”True” SizeObserver.ObservedWidth=”{Binding Width, Mode=OneWayToSource}” SizeObserver.ObservedHeight=”{Binding … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)