mario12136
Transition to Microsoft's Dependency Injection Library
I have a few questions about working with DI in WPF
1. Do I make all my views and view models services using
services.Add...<>()
.
2. Do I need to add models as services or should I avoid altogether having any contrusctors take models.
3. If I have a settings class that I want to access throught the entire, what is the best way to make it accessble. I tried this
and in any code I just say App.Current.Settings...
4 replies
❔ Settings model as a singleton in WPF
Hello,
thanks for the response. I have tried to read the articles you have attached. If I am not mistaken, the second one addresses how to get the values that I want from different sources and the first addresses how to manage groups of related settings. If my app is simple enough that I will only use a json file to serialize/deserialize and don't have "groups" of related options, is there a reason to still use these libraries.
In addition, a slight problem that I run into when trying to work with the code i have attached in the post is that to deserialize using the class i need a public contrstuctor, but since I am trying to make a singleton it shouldn't have one. Any advice on what to do here?
16 replies
✅ Would this be wrong MVVM?
Yes, the code-behind (regarding the last message). Just one more question if you don't mind. Where would I be passing the AvalonEdit as an argument. If I am not mistaken, since I set up things in the
App.xaml.cs
, creating the MainView
and the MainViewModel
, I would do something like
correct?20 replies
✅ Would this be wrong MVVM?
Thank you. This is what I understood
1) Create an interface lets call it
ITextEditor
.
2) Make my text editor control implement it
3) Make my MainViewModel
take in as a parameter an object of type ITextEditor
4) Pass the TextEditor
control into the MainViewModel
5) Create my editing commands in the MainViewModel
calling in the object that I passed.
Example:
where _textEditor
is of type ITextEditor
and the implementation is the actual text editor.
Please correct me if I am wrong. And also just to clarify, you think the other approach, which is making the commands in the text editor and binding to them like
is ok? In case they are both fine, is there a reason to prefer one over the other?20 replies
❔ Where are commands usually defined?
Thanks for the clarification.
I really like the idea of the mainview as an orchestrator.
Yeah the app is single-file editing, only .txt nothing like language or such (basically windows notepad before they introduced tabs). Plus I don't use a command panel, I rely on the menus which maybe arguably is the same. I subclassed AvalonEdit and implemented in it the functions that I needed such as selecting the current line and such. I was mainly wondering where to go from there and since there were several things that worked I was wondering what to do. For example, it could be as simple as puttinh [RelayCommand] on top of my functions in the TextEditor class / or a variation of having the commands in there and binding the menu items by specifying the ElementName and Path. I could have also created another class where I instantiated all the commands and did CommandBindings.add in the TextEditor class.
The only part about putting these commands in a ViewModel is how do I actually execute the functionality (working with lines, carets, indices since the ViewModel doesn't technically know I am working with AvalonEdit (texteditor code https://gist.github.com/MarioFares/368fa202f77bd363a906f129363e3a28).
30 replies
❔ Where are commands usually defined?
Thanks for the reply.
What I hope to gain is some good organization for the project. Basically it's like a notepad application, not very complex. The reason I want to avoid putting these commands in the MainViewModel is because I feel, organization-wise, there might be a better place to put them, I just don't know where that would make binding still easy. If I can actually have a file dedicated to just these commands that would actually be nice. Not sure if I should put them in the class/model of the texteditor.
30 replies
❔ Where are commands usually defined?
Thank you for you response. A follow up question if you don't mind:
If I have a main window with an editor and want to create editing commands like selecting, duplicating, deleting lines etc to which I can bind my menu items to, would I place these commands in the MainViewModel, create a ViewModel for the editor, or place it in the editor class itself?
I understand there are probably multiple ways to achieve this but I am just wondering if there is a standard/correct place to do this. (Thanks again)
30 replies