Get the View & ViewModel from a plugin - c#

I have an application which use plugins. Each plugin is developped following MVVM, so I have a View, binded to a View-Model inside. like the following picture:
In my application I have a Designer, when I add any ViewModel in my ViewModels list, its view will appear in the Designer.
My question is: How can I keep this binding when adding the viewModel of my plugin in my list ?? how to make its view appear in my designer ?

This is an interesting question... and like always there are many ways to accomplish it.
It depends what your plugins are.
If the plugin is more integrated and not so isolated. You should think about some Factory classes which are for example named like ViewResolver or ViewModelResolver. They can take parameters like the name as string, a type, a type of an interface or work by conventions.
Purpose is to find the VM for a view and vice versa as a central service. This service should also locate plugin VM or import them for example with MEF.
For more info’s google on view-first, viewmodel-first, view viewmodel marriage, etc.
Or
If your plugins are fully fleshed out components which run independently. I would suggest your host application should have in its viewmodel a list of components which are the plugins. So the Model will be a Component no matter if it contains the view and everything else because of the hosting app is to manage the components. So you would have an ObservableCollection which are bound to content control. The content control can then host the plugin as a whole.
These are two possible ways... like I mentioned above there is no "the" way in mvvm and it always depends on your use cases...
But I hope this guides you in the right direction...
HTH

Thank you for the answer #Silverfighter! it made me things more clear !
I found a very intersting article wich seems adapted to my problem
The solution is here:
http://www.alphablog.org/2012/05/07/simple-plugin-system-based-on-ninject-and-mvvm-light-2/

Related

WPF ApplicationCommands with PRISM

I'm redesigning an application to work with prism. The current application has some open/save/save as, ... routed commands implemented in a viewmodel that corresponds to a shellviewmodel. Usig a behavior the commands are added to the window's commandbindings. In the new design this logic doesn't belong in the shell but in a module.
To complicate matters this module doesn't have a view active before an 'open' command has been handled. So even if I were to add to the commandbindings of a view this would not work because there would be nothing to route to. I do have a view that is added to a ribbonbar but that works through an adapter that merges with the shell ribbonbar and detaches the merged elements from their old parent into the main ribbon, preserving the DataContext of each element (similar to https://prismribbonregionadapter.codeplex.com) It doesn't feel right to try and put add to the commindbindings there just to make it work.
This seems like a common problem and yet I'm not finding any information about it. Would it be a good idea to use a service for this somehow? Let the shellvm bind to the shell commandbindings and expose those through an interface? Any advice on how to tackle this?

Is it possible to decouple specific module implementation in MVVM, so that module functionality dont affect core?

my problem is that i have a CoreAPP, in this core app there possibility to active different modules (M1,M2,M3).
CoreApp have a view coreAppview. On that view if M1 is activated than i need to show an aditional button.
Right now there is just a simple if statement which just check config and add button if M1 is activated.
I dont feel very comfortable with just a single if statement between Core functionality and aditional module functionality and i want to be sure that some other developer dont just remove this check.
Are there any ways to decouple this functionality from the Core view in some way?
This can be done using plugin architecture.
You will need:
main view model. It will contain collection of loaded and activated plugins;
some way to load plugins (e.g. MEF);
DataTemplateSelector, which will load data template for plugin's view model. You can implement some convention-based search, e.g., if plugin's view model M1_ViewModel is placed in M1.dll, then load data template from M1\Views\M1.xaml;
items control (ItemsControl, ListBox, etc) in your main view. This items control will hold plugins collection and will use mentioned selector to load appropriate view.
A more generic solution would be, to add somthing like "GetAvailableActions" to the module's base class. Then, your CoreApp can call that method after a module was activated and generate the corresponding buttons in the view.

Open EditAppointmentDialog MVVM

I currently have a viewmodel such that it has a grid full of appointments. I would like to double click and open up my CalendarView with the editappointmentdialog opened for the record that was selected. May I ask how would I do that in an MVVM style?
I searched the internet and found this RadScheduleViewCommands.EditAppointment.Execute(appointment, this.scheduleView); but I don't have access to the scheduleView object from the MVVM. May I ask how abouts I should do this?
I think I can achieve this if I relay it back to the view, but I'm trying to look for another approach.
The issue you have is that you can't open a view from a view model as it'll break the MVVM design pattern.
There are a couple of ways you can open a view from the view model without too much hassle:
Implement a Messenger service to publish an event to the view, like MVVMLight does.
Straight up add a Click event handler on your button and open the view in the code behind (It's still view code after all).
However my preferred method is to use an ICommand, or RelayCommand. I have written a repository on GitHub which demonstrates how to achieve this.

How to make programmatic changes to views in the controller appear in the storyboard

I recently inherited a Xamarin project where I am to work on the iOS project. Going over several tutorial I figured I was ready, however the person before me did not use storyboards or controllers! They did all customization of views in files named LoginScreens.cs (basically controller files from the looks of it).
For learning purposes and ease of transition I would like to get a storyboard going in this project. So I created a storyboard titled Main and added a ViewController and essential copy and pasted the view customization code used in the LoginScreen.cs into my controller.
To be a little more specific, I have a Main.Storyboard that looks like this:
Where I am using editText boxes and a button to act as place holders for what I actually do to them in the ViewController.cs.
This all seems to register and builds properly however when I run the debugger on iPhone 6s iOS 9.3 I get the following:
The changes appear to show, but all my storyboard iOS designer views remain in place. I am trying to see if there is a way to reflect the changes made in the controller on the storyboard.
TL;DR: I'm trying to alter some views in a programmatic way in my ViewController.cs file. These views were originally added via the iOS designer and for customization purposes, they were edited in the controller. I want to see the visual alteration I make on a view in the controller, reflect in the iOS designer and when I debug.
Sounds like you are trying to go from a project where views were all done programmatically to implementing storyboards. This is a Big change and will take some time to convert the views over.
In the cs file there will be things like Add(passwordTextFeild) which are going to add more views to your storyboard view, hence why you get alot of views in your login view. You wont be able to see these in the storyboard as they are done at runtime.
If you are looking for IBDesignable this is more for custom controls and you still will have to add code to be able to see the changes from the .cs file in the storyboard.
Check my Question for IBInspectable/IBDesignable in Xamarin
Check this official link: https://developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_walkthrough/
Probably not what you wanted to hear but the UI in iOS projects tend to be done in one of the three methods:
All programmatically
Storyboards
Xibs
There are tons of questions/blog posts (even a video), weighting up the pros and cons of each. So possibly the last developer felt it was best to do it programmatically
There is a setting, IBDesignable, that you can add to the declaration of UI classes in Swift or Objective-C. That tells Xcode the those UI objects have a custom interface that you want to be presented in Interface Builder.
I have no idea how (or even if you could) you would use IBDesignable in Xamarin/C#. If its not supported for Xamarin then you're probably out of luck.
I suggest you search on "Xamarin IBDesignable" on the net.

MVVM like wizard

I am currently building an MVVM based application. The application should also have a wizard in MVVM style. The wizard is not a normal wizard, its a particular kind of a wizard. My goal is to implement a wizard with
1.) has also multiple branches. The wizard can guide you in other direction. So the wizard must not be straightforward.
2.) can also have short cuts. You can skip some pages where default values are setted.
3.) is also normal - straightforward.
Note, some information in the wizrad pages are on-the-fly. That means, that the information can be passed between each step and processed.
Are there any approaches like patterns to solve my problem? How do I implement it the best way?
Did you read this good article in Code Project about Wizard in MVVM and written by two MVVM guru:
http://www.codeproject.com/KB/WPF/InternationalizedWizard.aspx?display=Print
You might have a look at the ViewModel sample application of the WPF Application Framework (WAF). It shows how to implement a Wizard in a MVVM way.
If your wizard has a single VM that stores the state/results of each step and sits behind a view that is a user control...
You could have a Frame on the wizard view that requires 2 events in the code behind (This obviously depends on if your MVVM architecture can live with this?).
Event 1) When the binding of your wizards step raises its NotifyPropertyChanged: tell your frame to "Navigate" to the appropriate page (as described in a property in your wizard VM).
Event 2) On the frames "Navigated" event so that you can point the current pages data-context at your VM.
This way the wizard viewmodel controls the state of the wizard from start to finish and it also can describe the steps, which can easily be added to, edited, etc.
Obviously this may not sit well with everyone's view of MVVM.

Categories