Unable to register VM through Prism Navigation - prism

I'm creating WPF Core 3.1 app using Prism 7. In one of the view I am trying to register the view model for PRISM navigation through:
containerRegistry.RegisterForNavigation<ViewA, ViewAViewModel>();
And when required I do request to PRISM navigation service to navigate to the view using Region Manager
_regionManager.RequestNavigate(RegionNames.ContentRegion, "ViewA");
Navigation to the view is completed and I'm able to see the view on the defined region but the view model is not assigned to the view.
I tried to manually register the view model using the ViewModelLocationProvider but still the view model is not assigned.
ViewModelLocationProvider.Register<ViewA, ViewAViewModel>();
But if I use the PRISM Autowire property in the view then the view model is discovered and assigned to the view.
prism:ViewModelLocator.AutoWireViewModel="True"
View model class declared using IConfirmNavigationRequest interface required for navigation request handling
public class ViewAViewModel : RegionViewModelBase , IConfirmNavigationRequest
I am unable to figureout what I'm missing here.

The only thing I see wrong is that you would need to use the interface INavigationAware, not IConfirmNavigationRequest. I assume RegionViewModelBase implements BindableBase already.
public class ViewAViewModel : RegionViewModelBase, INavigationAware

Related

Xamarin Forms Prism: Is INavigationService necessary to be passed in constructor? Any other way apart from constructor injection

For prism framework in xamarin forms, to navigate from one view to another, is it mandatory to implement constructor inject and pass INavigationService in ViewModel's constructor?
I Can perform Navigation when I pass INavigationService as
public SomeViewModel(INavigationService navigationService)
{
_navigationService.NavigateAsync("SomeOtherPage");
}
But when I try to resolve whenever its needed, it doesn't work
public SomeViewModel(INavigationService navigationService)
{
ContainerProvider.Resolve<T>();// ContainerProvider is IContainerProvider
}
Is there any other way to access INavigationService apart from constructor injection in every Viewmodel
What you’re describing is an anti pattern, and generally just poor design. It also will not work because the Navigation Service is a very special service. It is created especially for each ViewModel as the Navigation Service must have the context of the Page you are navigating from. Without it, it would only work for resetting the Navigation Stack. To attempt to resolve the Navigation Service any other way in the ViewModel would likely break the MVVM design pattern.
If you want to use the Navigation Service you must inject it via the constructor. You may also simply use XAML Navigation in Prism 7.1. You can see a sample with the Prism Tests. You can also see this in practice in this demo app.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xaml="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
Title="{Binding Title}"
x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMockA">
<Button x:Name="testButton" Command="{xaml:NavigateTo 'XamlViewMockB'}">
<Button.CommandParameter>
<xaml:NavigationParameters>
<xaml:NavigationParameter Key="Foo" Value="Bar"/>
</xaml:NavigationParameters>
</Button.CommandParameter>
</Button>
</ContentPage>

Can't create Prism pages using "prism template pack"

When creating prism pages in Xamarin Forms app using the prism template pack I get the following error.
The parameter is incorrect.(Exception from HRESULT: 0x80070057
(E_INVALIDARG)
I am using VS2015, Prism Template Pack 1.7 and here's my project structure
My mistake was trying to create 'Prism' pages before modifying the app class to inherit from 'PrismApplication'.
I have just noticed that when using the template pack it automatically registers page for navigation within the 'RegisterTypes' method in the App class.
Adding to Muhammad's answer above, i also had to change the constructor to the following after inheriting from PrismApplication
public App(IPlatformInitializer initializer = null) : base(initializer)
{
}
And also had to change the Application to PrismApplication in App.xaml file also.

How to get reference to parent view controller in Cocoa?

I know in CocoaTouch there is the parentViewController property for this but there seems to no such property in Cocoa for OSX development. How can I ref the parent view controller in a sub view controller?
Nevermind! I'm an idiot! I've checked for this in a View class, not in a View Controller class. The NSViewController does indeed have a parentViewController property.

Binding to DataModel in MVVMCross

I have done the intro tutorial online and it works. My question is how does Mvvmcross know which View to associate with the ViewModel? There is nothing I can see in the code where they reference each other.
The default mode is by naming convention.
If your view is called "AwesomeView", Mvx will assign "AwesomeViewModel" to it at the app initialization.
When your view is instantiated, Mvx will "inject" the dependency (ViewModel) into your view as a binding context
So the convention to look up the view model is [ViewName]Model
If I´m correct this works using reflection

ViewModel with dependencies on Services + tombstoning

What is the recommended way to set up / inject dependencies in a viewModel after tombstoning given that when the app deactivates you typically add the ViewModel to the State dictionary and then before your app reactivates the framework deserialises the ViewModel which requires a default constructor?
If I have the class below, I would like to have dependency injection create an instance of "MyVM" injecting the dependencies for IServiceA and IServiceB. Having a default constructor would not set up the requried dependecies.
public class MyVM(IServiceA svca,IServiceB svcB)
{
}
How should the ViewModel be setup in a tombstoning scenario here?
I don't know if you're using a specific MVVM framework but Caliburn Micro has some built in features for tombstoning.
A little snippet from the docs:
public class PivotPageModelStorage : StorageHandler<PivotPageViewModel> {
public override void Configure() {
this.ActiveItemIndex()
.InPhoneState()
.RestoreAfterViewLoad();
}
}
That example is storing the ActiveItemIndex, a property on PiveotPageViewModel, in phone state but it can also store entire object graphs in PhoneState, AppSettings, or your own custom implementation. You get all that by inheriting from StorageHandler. With CM you don't have to worry about re-injecting services, it will handle that for you as it has it's own built in container.
CM WP7 Docs
In a recent Hanselman post about building a WP7 app he talked a bit about TombstoneHelper. I haven't used this one but it looks interesting.

Resources