mario12136
mario12136
CC#
Created by mario12136 on 7/26/2023 in #help
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
public IHost? AppHost { get; private set; }

public new static App Current => (App)Application.Current;

public Settings Settings => AppHost!.Services.GetRequiredService<Settings>();
public IHost? AppHost { get; private set; }

public new static App Current => (App)Application.Current;

public Settings Settings => AppHost!.Services.GetRequiredService<Settings>();
and in any code I just say App.Current.Settings...
4 replies
CC#
Created by mario12136 on 7/23/2023 in #help
What control should I use?
11 replies
CC#
Created by mario12136 on 7/23/2023 in #help
What control should I use?
@Denis Thanks for the response. I am looking to create something like Visual Studio's feature search in design (almost identical to it). I checked the radial menu but it is not exactly what I am after.
11 replies
CC#
Created by mario12136 on 7/6/2023 in #help
❔ Settings model as a singleton in WPF
oh I thought I should avoid a public constructor for preventing creating more than one instance
16 replies
CC#
Created by mario12136 on 7/6/2023 in #help
❔ Settings model as a singleton in WPF
I thought I'd make my contructor like
private SettingsModel()
{
string jsonString = File.ReadAllText(Path.GetFullPath("./Data/settings.json"));
JsonConvert.PopulateObject(jsonString, this);
}
private SettingsModel()
{
string jsonString = File.ReadAllText(Path.GetFullPath("./Data/settings.json"));
JsonConvert.PopulateObject(jsonString, this);
}
and use the Newtonsoft.Json instead of the built-int serializer in .Net. Would appreciate your thoughts on this.
16 replies
CC#
Created by mario12136 on 7/6/2023 in #help
❔ 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
CC#
Created by mario12136 on 7/6/2023 in #help
❔ How to use a readonly property for binding [AvalonEdit]?
If I make my property
public string SavedText
{
get => _document.Text;
set => OnPropertyChanged(nameof(SavedText)) // Just example
}
public string SavedText
{
get => _document.Text;
set => OnPropertyChanged(nameof(SavedText)) // Just example
}
and mark the binding as TwoWay, it works.
6 replies
CC#
Created by mario12136 on 6/29/2023 in #help
✅ Would this be wrong MVVM?
I understand. Thank you for this great discussion. I think I know what I need to do now.
20 replies
CC#
Created by mario12136 on 6/29/2023 in #help
✅ Would this be wrong MVVM?
Also maybe thoughts on making the MainViewModel partial and implementing different groups of related commands (File, editing, etc.) in different .cs files.
20 replies
CC#
Created by mario12136 on 6/29/2023 in #help
✅ 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
ITextEditor textEditor = new TextEditorService(MainView.TextEditor)
ITextEditor textEditor = new TextEditorService(MainView.TextEditor)
correct?
20 replies
CC#
Created by mario12136 on 6/29/2023 in #help
✅ 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:
[RelayCommand]
private void Cut() => _textEditor.Cut()
[RelayCommand]
private void Cut() => _textEditor.Cut()
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
<MenuItem Command={Binding ElementName=TextEditor, Path=SelectLineCommand}
<MenuItem Command={Binding ElementName=TextEditor, Path=SelectLineCommand}
is ok? In case they are both fine, is there a reason to prefer one over the other?
20 replies
CC#
Created by mario12136 on 6/29/2023 in #help
✅ Would this be wrong MVVM?
Thanks for the response, this is interesting.
20 replies
CC#
Created by mario12136 on 6/29/2023 in #help
✅ Would this be wrong MVVM?
Yeah the relay commands are in code-behind (I mean that's the text editor class anyway). I will bind to them my menu item's in my main view. Think of windows notepad.
20 replies
CC#
Created by mario12136 on 6/27/2023 in #help
❔ Where are commands usually defined?
Yeah I agree completely with the more effort part, yet I'll stick with MVVM
30 replies
CC#
Created by mario12136 on 6/27/2023 in #help
❔ Where are commands usually defined?
I think I might be doing things wrong
30 replies
CC#
Created by mario12136 on 6/27/2023 in #help
❔ 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
CC#
Created by mario12136 on 6/27/2023 in #help
❔ 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
CC#
Created by mario12136 on 6/27/2023 in #help
❔ 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
CC#
Created by mario12136 on 3/19/2023 in #help
❔ Rider false positive: Cannot resolve symbol...
I use ReSharper so the issue remains.
12 replies
CC#
Created by mario12136 on 1/16/2023 in #help
Move Line Up in C# TextBox like in VS
I took care of that with BeginChange() and EndChange()
5 replies