MVVM Page Navigation - wpf

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.

Related

Loading views from various modules upon pressing Ribbon buttons in PRISM

I'm developing WPF application using PRISM. I have two regions :
Ribbon region (which has got buttons to launch views from different modules)
View Region (in which views from different modules will be loaded but one view at a time)
I have separate module assemblies for Ribbon and views (i.e. CustomerModule, OrderModule, etc.). My ribbon has got buttons which should load view from modules i.e. pressing "Customer" button should load the specific view from "CustomerModule", pressing "OrderModule" should load the specific view from "OrderModule", etc.
How should I implement the command for my ribbon buttons to launch the view from different module assemblies? I'm not sure on how I can access the view module information into my ribbon module?
You can just call the RequestNavigate method of the ViewRegion from each button's command method as corresponds:
void OnCustomerButtonClicked()
{
this.regionManager.Regions["ViewRegion"].RequestNavigate(new Uri("CustomerView", UriKind.Relative));
}
You should then define every navigation request on each button's command method to properly navigate to the selected View.
You may find more information regarding Navigation on the following Prism Guide chapter:
View-Based Navigation
I hope this helps.

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.

How to control instantiation of View and ViewModel in 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.

Custom Menu UserControl WPF

I have created a custom Menu User control in WPF Browser application. How can I achieve Navigation from one page to other page using this Custom Menu User control from Main page. The pages would be displayed in other Frame control of the Main page.
There are different options to achieve this. Can you provide more details on how you're loading your UI? (i.e. Prism regions/modules, Data Templates, etc.)
My general suggestion would be one of two options:
Use a mediator pattern to have the menu control notify the frame control. MVVM Light has an easy to use Messenger implementation, which is a great toolkit to explore, in my opinion.
Have both controls contained in one "parent" control (or view model if you're using MVVM) which can then communicate to the frame control since it contains both menu and frame controls.
HTH,
Ben

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