How to create view based on MvxViewModelRequest for UWP? - c#

I am using:
C#
MvvmCroos.Core 5.6.3.
MvvmCroos.Platform 5.6.3.
Microsoft.NETCore.UniversalWindowsPlatform 5.2.2
VS 2015
Windows 10 target version: Build 14393
For Wpf approach MvvmCross supports CreateView behavior which based on MvxViewModelRequest returns a view as a FrameworkElement type.
Example:
var request = new MvxViewModelRequest(viewModelType, parameterBundle, presentationBundle, null);
var view = Mvx.Resolve<IMvxSimpleWpfViewLoader>().CreateView(request);
Unfortunately, CreateView and IMvxSimpleWpfViewLoader equivalents don't exist in UWP land. How do I achieve same functionality? Any suggestions?

In UWP a views is a Page. Pages are usually not created directly (although it is still possible), but rather displayed in the context of a Frame, which acts as a container and keeps track of navigation stack and navigation parameters for each page. With the container the Page also gets notified when it is navigated to and from using NavigatedTo and NavigatedFrom events.
If you check out the MvxWindowsViewPresenter code, you can see how navigation is handled in UWP. Instead of creating an instance of the page we use Frame.Navigate and pass in the type of the page that we want to navigate to and the Frame takes care of the lifetime of the page including its instantiation.

Related

Previous page instance not maintained after navigation in prism

I am using the latest version of Prism.MVVM in Xamarin.Forms. In this, if I try to navigate to second page from the first page, the first page is initialized once again. i.e., the constructor of the first page is called once again.
For example, I am having Page1.xaml and Page2.xaml pages with their respective view models(those will be created and registered automatically while creating in prism).
I navigating to Page2 from Page1 like below,
NavigationAsync("Navigation/Page1/Page2")
While navigating, Page1.xaml's constructor is called so that the page is created newly which lead I could not able to maintain the Page1.xaml instance. Also, please note that Page1.xaml is a Master-Details page.
Is this a behavior in Prism? If so how can I overcome this?
Thanks in advance.
Navigating away from a XAML page destroys it in UWP. You can preserve a page’s state data (and avoid re-construction) by adding a single line in a XAML page’s tag:
NavigationCacheMode="Required"
Does it work the same in Xamarin?

How can I dynamically update bindings in MvvmCross?

I know that bindings in MvvmCross get hooked up initially when we call SetContentView. However, I am dynamically creating a new view and the bindings from it are not being hooked up. Is there a way to get the MvvmCross bindings to hook up for views created after SetContentView was initially called?
In my example specifically - I am coding for the Google Glass client and implementing multiple cards. When certain information is sent to Glass, I create a new card and inflate an xml file for the new card's view but the bindings are not getting hooked up.
Code to create the new card:
_cardScrollAdapter.AddItem(LayoutInflater.Inflate(Resource.Layout.new_panel_view, null))
Portion of XML that creates the binding:
local:MvxBind="Bitmap BitmapConverter(PanelViewModel.Image); Visibility Visibility(PanelViewModel.ShowImage)"
The issue is related to the fact that you are passing a View inflated using the Android LayoutInflater.Inflate method, instead of using the MvvmCross BindingInflate method. With a using Cirrious.MvvmCross.Binding.Droid.BindingContext; at the top of your file, the following should work:
_cardScrollAdapter.AddItem(this.BindingInflate(Resource.Layout.media_panel_view,null));

Navigation between pages in WP8.1

I'm trying to migrate my app from WP8 to WP8.1. And I don't get how to navigate to already opened page with another parameters.
For example, I'm showing user info on UserPage giving it user's id as parameter. And when page is already is the content of the Frame I want to open UserPage again but for other user giving it another id.
My problem is that, using NavigationCacheMode set to Required for UserPage means that there will be no navigation with other parameters. But when NavigationCacheMode is set to Disabled navigation with another parameter is success but when I press back button old instance of UserPage is using data from new one.
In WP8 passing new parameters was enough to create new instance of a page with it's own cache. How to do similar in WP8.1 using WRT APIs?
Thanks to Romansz for the tip about using UserControl. Using UserControl binding to a ContentControl and handling BackKeyPress solves my problem with navigation.

User-control based program

I am trying to create a new Windows Store-app, but before creating too many user-controls, I will like to know something that I have no luck googling
If my program only have one page, and the code thereby is based by user-controls, could it then be possible to use GoBack, or should I implement my own way to do that?
And if I need my own way to override GoBack, can I then override GoBack in the Page, or should I override it in general for the whole app?
If you're going to keep the same instance of your page and just switch the user control that is displayed then you'll need to reimplement the existing navigation framework. For it to work you need to be calling Frame.Navigate() to switch between different page instances.
To do that in your case you could still only have a single page class but instead of just replacing the user control inside the same instance you could call Frame.Navigate() with the same page class and then inside it display the correct user control based on the parameter that you pass in. In this case you can use the existing navigation framework to navigate between the page instances.

Navgation between XAML pages

I have a simple Silverlight application that consists of four pages (XAMLs).
Navigation is done by calling:
//from XamlPageA
this.Content = new XamlPageB();
Is this the right way. I need to have the entries in Browser history so that users can go page to the previous page(s). How can I do it.
You are bypassing the navigation system completely by setting content manually. You would have to implement updating the browser history yourself if you do it that way (certainly possible, but quite tedious).
A simpler approach is to generate a "Silverlight Business Application" project and see how the page navigation is simply handled with hyperlink buttons. All the browser history plumbing is done for you as is the mapping from URL to views.
e.g. A button with NavigateUri="/Home" will cause a view named Home.xaml to load into the navigation:Frame of the MainPage window.
if you look into the navigation:Frame element of MainPage.xaml, you will see a number of UriMapping entries like this:
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
They provide the pattern matching to convert from URLs to views.
Hope this helps your project.

Categories