Create a ViewModel named 'MyViewModel'
Create a MvxContentPage named 'MyPage' (using MvxTabbedPagePresentation), like this -
namespace MYCOMPANY.MYPROJECT.Core.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[MvxTabbedPagePresentation(WrapInNavigationPage = false, NoHistory = true)]
public partial class MyPage : MvxContentPage
{
public MyPage()
{
InitializeComponent();
}
}
}
Everything works as expected.
namespace MYCOMPANY.MYPROJECT.Core.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[MvxContentPagePresentation(WrapInNavigationPage = true)]
public partial class MyOtherPage : MvxContentPage
{
public AssignLoadPage()
{
InitializeComponent();
}
}
}
Since the navigation to MyViewModel uses a naming convention, the above does not work ('MyOtherPage' does not match).
But I'm wondering if I can either -
Re-use the same MyPage and change the MvxTabbedPagePresentation to MvxContentPagePresentation at runtime?
OR create a second page 'MyOtherPage' that would use the same 'MyViewModel' ViewModel?
Some way not duplicate my code, creating a whole other 'MyOtherViewModel' - that does exactly the same as 'MyViewModel'.
Not really a bug, just wondering how to do this or if there's some documentation or example I missed?
Note: I can create a base viewmodel, and two separate viewmodels and pages - just wondering if there's a more elegant/easier solution.
Version: 5.5.2
Platform:
@ehuna Mvvmcross is an opinionate framework when it comes to navigation and one of the key aspects of it is that there is a one to one mapping between views and viewmodels (I personally believe if you are breaking this then you may have other architectural issues).
If you really do want to reuse the same viewmodel, my recommendation is to have a base view model and sub-class it twice, once for each of the views you want to use.
Hi @nickrandolph!
Yes, I agree the 1-to-1 View to Viewmodel mapping makes sense and I can certainly use a base viewmodel and sub-class it twice.
But in this case it really is the same View/Viewmodel - except that in one case it is inside a 'tab', while in the other it is not (standard navigation).
It would be great if at runtime, we could easily switch between -
[MvxTabbedPagePresentation(WrapInNavigationPage = false, NoHistory = true)]
and -
[MvxContentPagePresentation(WrapInNavigationPage = true)]
Is there any way to change the above without using attributes, but using code?
You can use the IMvxOverridePresentationAttribute interface to return a different attribute.
In https://github.com/MvvmCross/MvvmCross/pull/2483 this will be more useful because you have access to the viewmodel.
Most helpful comment
You can use the
IMvxOverridePresentationAttributeinterface to return a different attribute.In https://github.com/MvvmCross/MvvmCross/pull/2483 this will be more useful because you have access to the viewmodel.