Sharing the same viewmodel between pages/main page and sub-page - c#

I'm kinda new to the whole windows platform and I'm trying to create mvvm portable class library for windows and windows phone. Now that I have all my smaller pieces ready, I've found myself stuck at trying to figure out how to achieve the following with mvvm.
I have a main page where I can see the information for a car rental reservation and in this page. I'll be able to see a pickup location and a dropoff location (which are both inside a PointOfInterestViewModel). When I click on either the pickup/dropoff location. I want to switch to anther page which has a pivot with multiple list of possible locations. Once the user selects a location, I'll want to update the location in the PointOfInterestViewModel and then send the user back to the main view.
Is there a way where I can achieve it without having multiple PointOfInterestViewModel(s) and passing data between them? I would much rather have one single viewmodel but still adhere to mvvm.

You could put the PointOfInterestViewModel on the App class, bind the main form to it - then you could update it from the pivot:
((App)App.Current).PointOfInterestVM = ....

Related

Change WPF template/theme/UI/control at runtime

I'm very new to WPF. I have to create an WPF app which will do the following:
1) User can change the UI at run time. Controls in each UI are fixed. Suppose two textboxes for numeric value accept and command button to perform multiplication operation and alert the result.
2) Most importantly user can browse the folder of XAML files (UI) and select them / change then at run time. There can be 'n' number of UI users can create and use.
3) I want to use MVVM pattern.
I found this as best example but with doubt. Doubt is that, can user browse user controls and select then, set them? Do they need to be created and added to list before build see this ?
Which is the best way to achieve this task?
If you follow the MVVM pattern, you should create a View (implemented by an User Control) for each UI that your user selects. The Main View will have a ContentControl (or another control type that can host user controls like a HeaderedContentControl) which can host the other Views that the user selects. The view changes will be performed by the associated view model, just like the example you gave.
Trying to answer your question, you should create a menu where the user can select the View to be displayed. The existing views don't need to be added to a list before, because they will already exist in your project. If you want a more detailed example, with code, just update your question with some example you want, like the user can select two views from a menu for instance, and I will add some code .

Global MVVM ViewModel in UWP app

In WP8.1 or WPF desktop apps I could create my ViewModel as a static instance in App.xaml.cs and refer to it throughout the app as "App.myViewModel" but in UWP that does not seem to work (perhaps by design?).
The reason I want a global ViewModel is that, like many apps, I have one main page that populates the VM by downloading info from websites (I'm working on a business based RSS Reader for a client) and when I navigate to another page then come back to MainPage my vm is empty again (as I appear to have no option but to declare it in MainPage) so I have to reload all the data every time.
Also if I want to access the VM from navigated pages I now have to pass a reference to the VM through to that page whereas with the global approach I could still access App.myViewModel.
The only time I alter the VM contents is from UI actions on the Mainpage so I just want to be able to navigate back to the main page from other pages and have the VM still intact.
Can someone please explain how this is done in UWP? Can I still use App.xaml.cs and if so, how? If this is not possible why was it designed like this?
You are looking for ViewModelLocator. Here you have nice explanation and example how to use it. https://stackoverflow.com/a/14154088/4727426

How can I create field level help in Xamarin Forms MVVM project?

I have a Xamarin Forms project targeting iOS, Android and Window Phone. I am using MVVM for this project. All View Models are in a PCL and the UI is mostly in a shared project, such as ProjectUI. So, I have ProjectLib - the PCL, ProjectUI - the "shared" UI, ProjectUI.Android, ProjectUI.iOS, ProjectUI.Winphone.
Under ProjectLib, I have ProjectData - this defines all data objects that are used on both the client and server.
The app is generally running, but we have to come up with a way to add field level help to the app. To that end, we have added properties to the data that allow the user to define field level help.
If I have a property called FirstName, I would have another property called FirstName_Help. If FirstName_Help is non-blank, I'd like to change the style of the FirstName ("First Name:") prompt to show there is field level help for this field. Then, if the user taps the prompt, we'd like to pop-up a message with help text in it. If the FirstName_Help property is empty string, we do NOT want to change the prompt nor allow tapping on the prompt.
We have chosen this prompt method to reduce screen real-estate needed to depict there is help available, so I'd like to keep to this as our method if at all possible.
Because we are doing this via MVVM, I'd like to keep the code, if at all possible, in the View Model, not code behind. If we have to go with code behind, then we'll do that, but I'd love to keep it MVVM if at all possible.
I believe I can achieve the prompt change via a style, but I'm just not exactly sure how to go about doing it. The other challenge is how do I get the correct help text to display on the tap of the prompt?
I'd love to hear your thoughts on how this can be accomplished.
Thanks in advance.

Create Single Window Navigation in WPF

I want to create a kiosk mode application using WPF. I want to use a single window because the user should not be able to exit this fullscreen application.
The application should guide the user trough a process. When the user proceeds trough the process, the screen should constantly change and show him the next step.
Usually I just create a single window and use a "state machine" to switch UserControls containing the information for the current step. Is there a better way to achieve this functionality? Maybe I would get a better result using multiple windows or Pages (never used them).
The reason why I am asking is that in future I want to have a simple, clean way of switching the content inside a single window. i.e. I am planning to implement some sort of animation when switching content (like sliding to the next / previous step). I don't want to put more effort into my current approach if it isn't the most flexible and clean one.
Any ideas?
PS: This is about desktop applications. Today I come from the Winforms environment and am experimenting with WPF.
There's a few ways you can achieve this.
First would be to use a Page based application, this will allow you to use a single window. Here is a pretty interesting tutorial
A bonus of using this approach is that navigation between pages is built in.
Your requirements are that you need to use animation for transitioning between pages, as far as I'm aware, using a Page based application cannot achieve this (I may be wrong). So your other option would be to use a UserControl MVVM approach.
This probably won't make a lot of sense now, but here goes:
You can use a single master view model which will hold multiple child view models, each of these could have a visibility property which dictates the visibility of the associated view. The master view model would simply be responsible for displaying the appropriate view model depending on where the user currently is in the application.
Using some clever XAML, you can create storyboards (animations) when the view becomes visible, which will allow you to achieve the crazy awesome animations that you require.
Anyway, that probably didn't make any sense, so here's a tutorial to get you started with MVVM.

MVVM shared between Win8 and WP8 (2 pages in one page)

We have a Windows Phone 8 application that uses MVVM. There are a couple of pages that work in the following way. Page 1 has some buttons and when one of them is clicked, page 2 is opened.
Now we want to migrate our application to Windows 8. Because of the bigger screen size, we can actually display both the buttons and the content on the same page (see the attached picture). Basically we are going to have a single screen on which we dynamically change a part of it (the right side with green)
We would like to share the view models between the two application. Is there a pattern to use for something like this?
There are a few alternatives that we investigated but we are not sure which one is correct:
Should "some content" (the second page in WP8) be a user control in the windows app?
Should we create a completely new view model for the Win8 that has the code from multiple view models in the WP8 app? This sounds like a bad idea though because we have about 6 view models in the WP 8 app that are now displayed on the same page in the win app.
Should the Win 8 view models somehow "contain" the other view models?
Should the dynamic content (the green box) have its own MVVM?
Ideas? Suggestions?
Well, this depends too much on your application to give any definite answer. But since pressing the buttons will load different content in the W8 app, MVVM can solve this without too much problems.
So, in WP8 app pressing the buttons issues a message that triggers navigation. In W8 you can change that message handler to destroy user control and create new one. In WP8 app your second screen was a standalone page with its own view model, there is no reason to not have a user control on W8 using that same view model.
However, you have to write UI from scrath since XAML cannot be shared.

Categories