Page navigation in WPF using MVVM - c#

I am using a NavigationWindow to house my pages in this WPF application. I am just learning MVVM with WPF and have been looking for a good solution to navigating pages while maintaining MVVM.
I have a LoginViewModel that does all SQL database credential checking, once the credentials have been verified against a Password Hash I want to navigate to a new page depending on who logs in.
I am looking for some suggestions or common answers to this solution. I have done a lot of research and keep seeing IOC containers or an application view model? I wanted to get some other opinions on the matter and possibly a point in the right direction!
Thanks!

I would suggest using a framework to achieve the IOC container solution. Mvvm light for example uses a ViewModelLocator to store its ViewModels.
An example for page navigation in MVVMLight can be found here.

Related

Xamarin Forms : using Prism and/or ReactiveUI, is it possible to achieve vertical slicing?

I have been working on a mobile application for one week, for personal education and enhancement. I have some experience with WPF and MVVM, I have no experience with mobile development.
The idea is to make a cross platform application, while testing some coding philosophies :
Reactive programming (ReactiveUI)
Vertical slicing, if possible (blog 1, blog 2)
I plan to use the following components :
Prism.Unity.Forms : seems to provide application structure, ioc, navigation, seems widely used, and done by top notch people
ReactiveUI, ReactiveUI.XamForms, ReactiveUI.Fody : WhenAnyValue, ObservableAsPropertyAttribute, ReactiveAttribute (tutorial), not using them looks like a huge missed opportunity to me
The current structure of my Visual Studio is the following :
Csproj
ViewModels
CreateExercisePageViewModel
HomePageViewModel
Views
CreateExercisePage
HomePage
I managed the following :
Set the HomePage as the initial Page, contained inside a NavigationPage, by using the navigation functionalities of Prism.
Declare a named button in XAML, set its Command property through a OneWay binding created in code behind, using ReactiveUI functionalities (like shown in the tutorial, using the WhenActivated method)
Navigate to the second page (CreateExercisePage), using Prism navigation
One issue is that, even though I have seen a few articles regarding vertical slicing, there were mostly about web applications.
So the question is : Is vertical slicing applicable to mobile applications ?
I would like to restructure my project by using vertical slicing, but having troubles finding articles about Xamarin Forms, I feel uneasy.
Edit : my understanding of the vertical slicing concept, is that you should group code, related to a feature, in the same physical space (folder). By doing so, it gives developers a huge hint about the cohesion of those files.
Given a mobile application is made of screens, I assumed grouping code by screen, regardless of their type (presentation, logic, persistence) would make sense.
Rewriting the application would give the following csproj, then :
Csproj
Screens
Home
HomePage
HomePageViewModel
CreateExercise
CreateExercisePage
CreateExercisePageViewModel
Adding a new screen would mean adding a new folder to the "Screens" folder, reducing the risk of modifying existing files/logic.
Vertical Slicing is a little bit like MVVM which we are utilizing on Xamarin.Forms:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm
The view is the UI layer, view model helps you handle the logic code and model represents your database data.
For instance, a user clicks the button on view layer to trigger a command in the view model. Then this command could add a new product to the List property of that view model. As this list property has been changed it will notify the UI to respond to this action.
Moreover, your first architecture is more appropriate:
ViewModels
CreateExercisePageViewModel
HomePageViewModel
Views
CreateExercisePage
HomePage
Make your pages separated by your view models.

Navigation MVVM

My attempts at trying to stick with the MVVM design pattern has left me spinning in my tracks. I have a view that is a pick list of site locations. I have a viewmodel that the view gets its data context from and I've bound some items to it with buttons and such (yay!).
Now I'm trying to switch the current view from a button click from within the view.
By clicking on the "start maintenance" button I would like to switch the view to another view with a different viewmodel.
So I know MVVM is just a design pattern and there seems to be a bajillion different ways of implementing navigation with MVVM. But the majority of these solutions I've seen point to having a main navigation pane and that is not what i am intending to do.
I plan on now trying to use a viewmodellocator with MVVM light messenger pattern to try and get my views to change. But after spending the past 3 days trying to shoehorn this thing to work, I'm growing desperate. Are there any other suggestions on how to implement this? I do like Sheridan's answer to a similar post:WPF MVVM navigate views because it avoids using a toolkit/framework. But I think the answer was perhaps too vague for me and I couldn't implement it because I didn't understand how to change the views (Sheridan's custom relay message was hard to follow as a novice).
Help please you Internet Denizens! :) If you could point me to any examples that would appreciated as well!
A big thank you to Rachel! I created a mainviewmodel that instantiates the other view models in my app. Then I used MVVM light to send a message using a template of a generic object to stuff the message between the models which then allowed me to handle the message in my main view model and switch the viewmodel context!
Thanks for all the help!

Page, Frame, Navigation windows in C# WPF

I would like to know the difference between page, frame, navigation windows in c# wpf
what is the best choice of them for wpf windows application?
in my application how to make fixed part (contain main buttons) and changeable part (show pages) after clicking buttons in fixed part
are there any good websites provides video tutorials for c# wpf from beginning to professional?
thank you
A Page is much like a user control, only that is is displayed within a Frame, which again is part of a NavigationWindow. A NavigationWindow is a special kind of window that allows for page navigation and can display the respective controls for navigating pages.
A paged application is a good choice if you want Wizard-like functionality, or if the user experience should be comparable to what you get when browsing the web. In many cases, using standard WPF windows is a better choice.
The NavigationWindow already contains a "fixed part" that can contain controls. You can also use a normal window, place a Frame in it and then - through proper layout - create your own "fixed parts". Navigation would then come down to calling the navigation methods the Frame provides.
From the answer to this question:
Pages are intended for use in navigation applications (usually with back and forward buttons, e.g. Internet Explorer). Pages must be hosted in a NavigationWindow or a Frame
The best choice depends on what kind of application you want to create. Is it a wizard or navigation type application or just a regular application with one window (maybe with tabs)?
I would definitely consider using a MVVM framework like Caliburn.Micro for making a WPF application. It has some really powerful mechanisms for dealing with Screens, Conductors and Composition, in addition to encouraging you to decouple your application by using the MVVM pattern. The author of Caliburn.Micro, Rob Eisenberg, has written some tutorials with extensive explanation about and how to use the framework under the project's documentation. There is also lots of resources around the interwebz, google it! :)
I can also recommend Pluralsight's WPF and XAML Fundamentals and WPF Advanced Topics, they should cover what whatever is worth knowing about WPF :)

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.

Controller/Static State Class in WinForms Application - Where to put?

I'm writing a WinForms application and want to have an "MVC-Type" Design. Actually it's more MVP or MVVM,.
The plan is to have a Central Controller which does all the actual work, so that the Forms just render out ViewModels and handle user input, but everything that actually does something goes through the Controller.
I just wonder if this is a good idea, and where to put the Controller? The current idea is to have a static class which is initialized in Program.cs (Sending in some Dependencies like IMyDatabaseRepository) so that it just stays a controller that delegates work between User Interface and Model.
As you might guess, I come from a Web Background and have little experience with WinForms architecture. Previously, my MainForm was the Controller class, holding all the State Variables, which obviously means that my MainForm is my application rather than just a part of the User Interface.
Nice question Michael!
Here are some links:
Sacha Barber's WPF MVVM VS Project Template
Sacha's Article Series on CodeProject.com
Nice article on this Wordpress blog
Hope these help you to structure your project properly!
I don't know if this is a better way, but I am having Structuremap create my controller and database instance.
The main form has no real code in it - it just loads the first set of controls and then starts the controller. The user controls on the form use StructureMap to access the controller.
My project is regular WinForms and not WPF and is my first time using the MVC pattern with WinForms.
You might have a look at the WAF Windows Forms Adapter download. It comes with the BookLibrary sample application which uses a Controller / MVVM design together with Windows Forms.

Categories