I've read through discussions on opening a dialog using mvvm pattern. I've seen several examples that say to use a service, but I'm not understanding how all the pieces fit together. I'm posting this question asking for guidance in what I should read up on to better understand what I'm missing. I'll post what I have below, and it does work, but from what I'm seeing in these posts i'm not doing it right, or maybe not doing efficiently. I see where a dialog interface is created, then a class that uses the interface to do the actual work. Then in the ViewModel a constructor passes in this interface, this is the part that really confuses me, not sure what's passing it in, don't have enough information to connect the dots and not sure what i'm missing.
Here is one post I looked at: https://stackoverflow.com/a/1044304/4593652 I kind of see what they are suggesting, but it's not enough information for someone learning. I'm not asking anyone to write this for me, just hoping for some advice on what I should read up on to understand how these pieces fit together.
In my code I have a class like this: (This works, I just feel like i'm missing something from what I'm reading on other posts)
public class OpenDialogService
{
public string GetOpenDialog(string title)
{
CommonOpenFileDialog dlg = new CommonOpenFileDialog();
dlg.Title = title;
dlg.IsFolderPicker = true;
dlg.AddToMostRecentlyUsedList = true;
dlg.AllowNonFileSystemItems = false;
dlg.EnsureFileExists = false;
dlg.EnsurePathExists = true;
dlg.EnsureReadOnly = false;
dlg.EnsureValidNames = true;
dlg.Multiselect = false;
dlg.ShowPlacesList = true;
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
return dlg.FileName;
}
return null;
}
}
Then I use this in my ViewModel when my command is called.
path = new OpenDialogService().GetOpenDialog("...");
Walther is basicly right.
The other post says that he could change his service implementation via IOC Container. IOC means the "Inversion of Control"-Pattern, you could read this article to get a basic overview. I think "Service" comes also from that Pattern. Another buzzword is Dependency Injection you can look for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With