Reputation: 1477
I am using prism5 with regionManager. Following is how I registered my views and how I am trying to navigate.
_container.RegisterType<IMyView,MyView>("MyView");
and this is how I am navigating
_regionManager.RequestNavigate("MyViewRegion", new Uri("MyView", UriKind.Relative);
This one navigates to MyViewRegion but only shows System.Object
Some say to fix this by registering the views as follows
_container.RegisterType<object,MyView>("MyView");
But I still want to register my view with an interface type. So how could I fix this with RequestNavigate();
Thanks
Upvotes: 2
Views: 2228
Reputation: 183
Try to register your views in Module.cs/App.xaml.cs:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<ViewB>();
}
And your problem will be resolved :)
Upvotes: 0
Reputation: 145
I modified your code a bit and I am loading the code inside my module class Initialize method. Most of the other suggestions online were no help for Prism 5.
public void Initialize()
{
_regionManager.RegisterViewWithRegion("MainNavigationRegion",
typeof(Views.TestDocumentNavigationView));
LoadViewInRegion<TestDocumentView>("MainRegion");
}
/// <summary>
/// You must load the views in order for the navigation to resolve
/// </summary>
void LoadViewInRegion<TViewType>(string regionName)
{
IRegion region = _regionManager.Regions[regionName];
string viewName = typeof(TViewType).Name;
object view = region.GetView(viewName);
if (view == null)
{
view = _container.Resolve<TViewType>();
region.Add(view, viewName);
}
}
Upvotes: 0
Reputation: 637
I used to get this problem which was caused when attempting to navigate to a view that was not loaded. I therefore use a simple function that I call before I call the .RequestNavigate method to check the view has been loaded:
private void LoadViewInRegion<TViewType>(IRegion region, string viewName)
{
object view = region.GetView(viewName);
if (view == null)
{
view = _container.Resolve<TViewType>();
region.Add(view, viewName);
}
}
So the code to display the view would be something like:
IRegion region = _regionManager.Regions["MyViewRegion"];
LoadViewInRegion<IMyView>(region, "MyView");
_regionManager.RequestNavigate("MyViewRegion", new Uri("MyView", UriKind.Relative);
Upvotes: 0