How to control instantiation of View and ViewModel in Silverlight? - silverlight

My silverlight application has 2 pages, PageA and PageB. Each page is binding to it corresponding ViewModel, PageAViewModel and PageBViewModel. (I set ViewModel to View's DataContext inside xaml.)
User can switch back and forth between these pages, when user switch between these pages they create new instance of PageA and PageB which also create new object of its ViewModel which I don't want. I try to set NavigationCacheMode to Enable, now all Views create instance only first time user navigate to that page this also mean ViewModel has only one instance.
I want to know how to control UI to create only one instance of View and control when to create new instance of its ViewModel?

This is where IOC (Inversion Of Control), like Unity, comes in handy.
You would simply register the ViewModel as a singleton (one only, ever) with Unity.
Rather than embed a ViewModel in the View (really bad practice to hardwire them like that ), you specify what type of ViewModel the View wants when it is created and the matching ViewModel would be "injected" into the view.
Basic MVVM, without some other injection framework, does not give you much out of the box (aside from a separation from the view).

Have you tried using MVVM light framework for your application. It supports a view model locator concept which avoids recreating the instance of your VM each time a user clicks on a page. Take a look at a presentation by Laurent Bugnion here.

Related

iOS8 Universal storyboard how to refactor split view controller using size classes on iPhone?

I've converted my storyboard that uses split view controller to size classes using xCode 6 beta 3. Upon running the application, I see that the detail panel shows up first and has a "back" button in navigation. That button brings me to the master view. However, I cannot navigate back to detail view controller from master.
Is there a way for me to keep using master/detail approach in a universal storyboard?

MVVM Page Navigation

Have begun a WPF app following mvvm pattern and have hit an issue. I have a Customers page which has a number of searches and returns a list of customers. When I double click a record I want to be able to navigate to the Customer view so user can view/edit details.
Is this possible without using MVVMLight or WAF or PRISM(as I have struggled for a bit getting head into PRISM fully!!!) without forcing my view model to have knowledge of my application?
Thanks
Some people use Dependency Injection to connect VMs with Views in a decoupled way. Take a look at Unity
I would use a seperate / underlaying shell view & viewmodel with just the structure of your ui layout (some grids for positioning navigation, menu, search or content areas)
Then position your application views / controls the shell view. (maybe with some Visibility Bindings)
The main purpose of the shell viewmodel is to coordinate the flow of your ui. For example what control should hide or show based on some events of its child controls.
You could use an eventhandler in your search, wich is registered in the shell viewmodel, to show / hide your different content views.
Another approach could be a ContentPresenter.

Making a Wizard in Silverlight + Xaml

I have a Silverlight application and I am trying to make each step of a wizard in XAML files, instead of hard-coded C#.
The problem is that I don't understand how I am going to switch between them after click on next button of each screen.
What is the best way to do this? I saw some tutorials on the internet about XAML dynamically loaded but none of them seem to work with me :/
Use a ChildWindow as your parent window. Then create multiple UserControls which will be framed in the content of the parent window. In the code-behind of the parent window, load the user controls into a list and set the visibility to 'Collapsed' for all of them but the first. When the user presses the Next/Prev buttons, pull the appropriate UserControl from the list (keep track of the current index) and make it 'Visible' while making the current control 'Collapsed'.
All of your navigation code will be in the parent window, but the parent window won't be concerned about the content of the wizard steps itself.
Each UserControl can be coded in XAML as a separate control so you still maintain a separation of the control from your wizards navigation logic.
You can then create a class type that will hold all of the options for the various wizard controls. Pass a reference to an object instance to each of the controls for them to update. When you get to the end of the wizard, your option object should maintain the state of all the wizard steps and the parent window can return that to the application.
I would suggest looking into the Silverlight Navigation Framework. It allows you to use "urls" to navigate between "pages" (which are your XAML user controls). It also also users to use the back and forth buttons in the browser, which may or may not be something you want to allow.
There is a VS 2010 template when you choose New Project, Silverlight, "Silverlight Navigation Application" that will help get you started.

Setting the ViewModel of a View when using Navigation Service in Silverlight

I'm am having trouble finding out how I would create an instance of a view model and set that as the view model of a view am I am going to navigate to using the Silverlight navigation framework.
for instance, If I have a list view with a view model, I would like the list view to use the navigation service to navigate to the details view and and set the view model of the details view to the same instance of the view model that the list view is using.
From what I have read and understand, there is no way to pass data along when using the Navigate method. And the navigate method does not return the instance of the view that will be navigated to?
So my question is... Once I have used .Navigate(URI) to navigate my silverlight application to the detail view, how can I set the viewmodel of detail view to the instance in list view before i navigate away from the list view.
First of all, your statement "From what I have read and understand, there is no way to pass data along when using the Navigate method" is incorrect. You can pass simple data values via parameters.
You can also share a datacontext between views by using a navigation frame. Check this SL3 article out:
http://timheuer.com/blog/archive/2009/04/03/share-data-between-navigation-pages-in-silverlight-3.aspx
You can use the query string to pass parameters to the view you want to navigate to, then use those parameters to create the ViewModel.
here is a post about "Site Navigation Basics in Silverlight 4" that shows how to use query string parameters in Silverlight navigation.

Switching views in a Silverlight Mvvm application

I have a View where the user can select "Basic Configuration" and "Advanced Configuration" from a radiobutton group. This view will then display one of two usercontrols (views): BasicConfigurationView and AdvancedConfigurationView. I can solve this by just hiding/showing the views when the user clik on the radiobuttons, but is there a better way? It would have been perfect if the AdvancedConfigurationView was not instantiated at all if the user chose the BasicConfigurationView.
Take a look at Prism or what Microsoft also calls Composite WPF. With Prism you can dynamically download, instantiate modules (user controls) and inject them into pre-defined regions in your views.
Here is the link. http://www.codeplex.com/CompositeWPF

Resources