Issues Encountered with Region Initialization in WPF Forms

21 views Asked by At

I'm utilizing Prism.Unity-8.1.97. I've registered my view and ViewModel as follows:

public class AppModule : IModule
{
    private readonly IRegionManager regionManager;
    private readonly IUnityContainer container;

    public AppModule(IUnityContainer container, IRegionManager regionManager)
    {
        this.container = container;
        this.regionManager = regionManager;
    }
    public void OnInitialized(IContainerProvider containerProvider)
    {
        var regionManager = containerProvider.Resolve<IRegionManager>();
        regionManager.RegisterViewWithRegion("MyViewRegion", typeof(MyCustomView));
        var asdf = containerProvider.Resolve<MyCustomViewModel>();
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {

        containerRegistry.Register<IMyCustomView, MyCustomView>();
        containerRegistry.Register<MyCustomViewModel, MyCustomViewModel>();
    }
}

//My view structure is as follows:


<Window x: Class = "PrismTestingApp.MainWindow"
        xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
        xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns: local = "clr-namespace:PrismTestingApp"
        mc: Ignorable = "d"
        xmlns: prism = "http://prismlibrary.com/"
        Title = "MainWindow" Height = "450" Width = "800" >
    <Grid>
        <ContentControl Grid.Row = "0" prism: RegionManager.RegionName = "MyViewRegion"/>
    </Grid>
</Window>


//My MyCustomViewModel for "MyCustomView" is like below

using System.Collections.ObjectModel;
using System.Windows;
using PrismTestingApp.Interfaces;

namespace PrismTestingApp.ViewModels
{
    public class MyCustomViewModel : Prism.Mvvm.BindableBase, MyCustomViewModel
    {
        private readonly IMyCustomView View;

        private string _labelTitle = "Test label with viewmodel";

        public string LabelTitle
        {
            get { return _labelTitle; }
            set { SetProperty(ref _labelTitle, value); }
        }

        public MyCustomViewModel(IMyCustomView View)
        {
            this.View = View;
            View.DataContext = this;
            Raise_triggerEvent();
        }
        public static event EventHandler triggerEvent;
        public static void Raise_triggerEvent()
        {
            triggerEvent?.Invoke(null, EventArgs.Empty);
        }
        private ManualResetEvent updateEcosystemsMRE = new ManualResetEvent(false);

    }
}

//App.xaml.cs code as follows
 public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        MyBootstrapper _Bootstrapper = new MyBootstrapper();
        _Bootstrapper.Run();

    }
}

//Problem statement: When I open my page like below:


public class MyBootstrapper : PrismBootstrapper
{
    MainWindow mainWindow;

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        base.ConfigureModuleCatalog(moduleCatalog);
        moduleCatalog.AddModule(typeof(AppModule));
    }

    protected override DependencyObject CreateShell()
    {
        return mainWindow = new MainWindow();
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Application.Current.Dispatcher.BeginInvoke(new Action(async () =>
        {
            await InitializeModulesAsync();
        }));
    }

    private async Task InitializeModulesAsync()
    {
        try
        {
            base.InitializeModules();
            mainWindow.Show(); //showing Window form
        }
        catch (Exception ex)
        {
           
        }
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
    }

    public event Action BootstrapComplete;
}




//The region is once again creating an instance of "MyCustomView". Why isn't it utilizing the instance that was already registered by the IOC container? Could someone assist me in identifying this problem?

The regions specified within the <ContentControl Grid.Row="0" prism:RegionManager.RegionName="EcosystemsViewRegion" /> element in the window form should refrain from attempting to reconstruct the "EcosystemsView" again. Instead, they should utilize the instance of "EcosystemsView" that has already been created.

0

There are 0 answers