I am using Prism for Xamarin Forms and Implemented Modularity in the applications. I have .Droid Project, Default PCL Project which has App.xaml file and other content pages. And I have 4 PCL projects which are created as Modules by implementing IModule. Modules are configured in the App.xaml.cs.
My problem is when app initialized , the login page, home page which are in default PCL project loaded properly. But any content page in Modules are Initializing exactly three times every time I navigated to it using _navigationService.NavigateAsync("") from another page either from the same module or the other module. Content page constructor was called three times along with the corresponding ViewModel constructor, but the OnNavigationCompleted in VM is running only once.
May be because of this issue or some other reason, my Content page in the module is taking few more fractions to load fully. This can be easily noticed while loading. Overall Navigation of Content Page in the module is not so smooth.
Is there any solution for this? I have already tried keeping an empty content in the module page without any controls and the result is same.
Below are the code snippets of my project.
Configure Module in app.xaml.cs
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
Type module1 = typeof(QNPL.Mobile.Module1);
Type module2 = typeof(QNPL.Mobile.Module2);
Type module3 = typeof(QNPL.Mobile.Module3);
Type module4 = typeof(QNPL.Mobile.Module4);
ModuleCatalog.AddModule(
new ModuleInfo()
{
ModuleName = module1.Name,
ModuleType = module1,
InitializationMode = InitializationMode.OnDemand
});
ModuleCatalog.AddModule(
new ModuleInfo()
{
ModuleName = module2.Name,
ModuleType = module2,
InitializationMode = InitializationMode.OnDemand
});
ModuleCatalog.AddModule(
new ModuleInfo()
{
ModuleName = module3.Name,
ModuleType = module3,
InitializationMode = InitializationMode.OnDemand
});
ModuleCatalog.AddModule(
new ModuleInfo()
{
ModuleName = module4.Name,
ModuleType = module4,
InitializationMode = InitializationMode.OnDemand
});
}
private async void MenuItemClick(object value)
{
if (value != null)
{
MenuDetail menuItem = (MenuDetail)value;
SelectedMenu = menuItem;
if (!string.IsNullOrEmpty(SelectedMenu.ModuleName))
{
_moduleManager.LoadModule(SelectedMenu.ModuleName);
}
await _navigationService.NavigateAsync(SelectedMenu.URL, navParams);
}
}
Module Page:
using Microsoft.Practices.Unity;
using Prism.Modularity;
using Prism.Unity;
using QNPL.Mobile.Module1.API;
using QNPL.Mobile.Module1.Views;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace QNPL.Mobile.Module1
{
public class Module1 : IModule
{
private readonly IUnityContainer _unityContainer;
public Module1(IUnityContainer unityContainer)
{
_unityContainer = unityContainer;
}
public void Initialize()
{
_unityContainer.RegisterTypeForNavigation<Module1HomePage>();
_unityContainer.RegisterPopupNavigationService();
_unityContainer.RegisterType<IApiInterface, ApiInterface>();
}
}
}
Content Page in Module1:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="QNPL.Mobile.Module1.Views.Module1HomePage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:prismBehaviors="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
x:Name="Module1HomePage"
Title="Home Page"
prism:ViewModelLocator.AutowireViewModel="True">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
ContentPage .cs file:
using Xamarin.Forms;
namespace QNPL.Mobile.Module1.Views
{
public partial class Module1HomePage : ContentPage
{
public Module1HomePage()
{
InitializeComponent();
}
}
}