WPF Prism.Wpf, Prsim.DryIOC integrate sub module/project into main module via IRegionManager

Get-Project -All| Install-Package Prism.Wpf -v 8.1.97;
Get-Project -All| Install-Package Prism.DryIOC -v 8.1.97;

 

//MainWindow.xaml
<Window x:Class="WpfApp48.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:WpfApp48"
        mc:Ignorable="d"
        WindowState="Maximized"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding MainTitle}" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ContentControl Grid.Row="0"
              Grid.Column="0"
              prism:RegionManager.RegionName="ViewA" Background="LightBlue"/>
        <ContentControl Grid.Row="0"
              Grid.Column="1"
              prism:RegionManager.RegionName="ViewB" Background="LightGray"/>
        
        <ContentControl Grid.Row="1"
              Grid.Column="0"
              prism:RegionManager.RegionName="ViewC" Background="LightCyan"/>
        <ContentControl Grid.Row="1"
              Grid.Column="1"
              prism:RegionManager.RegionName="ViewD" Background="LightSalmon"/>
    </Grid>
</Window>


//BookProjectModule.cs
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace BookProject
{
    public class BookProjectModule : IModule
    {
        IRegionManager regionManager;
        public BookProjectModule(IRegionManager regionManagerValue)
        {
            regionManager = regionManagerValue;
        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewA"].Add(containerProvider.Resolve<BookView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<BookView>();
            containerRegistry.Register<BookProjectModule>();
        }         
    }
}

//CarProjectModule
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace CarProject
{
    public class CarProjectModule : IModule
    {
        IRegionManager regionManager;
        public CarProjectModule(IRegionManager regionManagervalue)
        {
            regionManager=regionManagervalue;
        }
        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewB"].Add(containerProvider.Resolve<CarView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<CarView>();
        }
    }
}


//HouseProjectModule
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace HouseProject
{
    public class HouseProjectModule : IModule
    {
        IRegionManager regionManager;
        public HouseProjectModule(IRegionManager regionManagerValue)
        {
            regionManager = regionManagerValue;
        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewC"].Add(containerProvider.Resolve<HouseView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<HouseView>();
        }
    }
}


//StudentProjectModule
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace StudentProject
{
    public class StudentProjectModule:IModule
    {
        IRegionManager regionManager;
        public StudentProjectModule(IRegionManager regionManagerValue)
        {
            regionManager = regionManagerValue;
        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewD"].Add(containerProvider.Resolve<StudentView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<StudentView>();
        }
    }
}

//MainProject/App.xaml.cs
using BookProject;
using CarProject;
using HouseProject;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using StudentProject;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApp48
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);
            moduleCatalog.AddModule<BookProjectModule>();
            moduleCatalog.AddModule<CarProjectModule>();
            moduleCatalog.AddModule<HouseProjectModule>();
            moduleCatalog.AddModule<StudentProjectModule>();
        }
    }

}

 

 

 

 

 

 

 

 

image

 

 

 

 

 

 

 

 

image

 

 

 

 

 

 

 

 

image

 

 

 

 

//MainWindow.xaml
<Window x:Class="WpfApp48.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:WpfApp48"
        mc:Ignorable="d"
        WindowState="Maximized"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding MainTitle}" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ContentControl Grid.Row="0"
              Grid.Column="0"
              prism:RegionManager.RegionName="ViewA" Background="LightBlue"/>
        <ContentControl Grid.Row="0"
              Grid.Column="1"
              prism:RegionManager.RegionName="ViewB" Background="LightGray"/>
        
        <ContentControl Grid.Row="1"
              Grid.Column="0"
              prism:RegionManager.RegionName="ViewC" Background="LightCyan"/>
        <ContentControl Grid.Row="1"
              Grid.Column="1"
              prism:RegionManager.RegionName="ViewD" Background="LightSalmon"/>
    </Grid>
</Window>

//App.xaml
<prism:PrismApplication x:Class="WpfApp48.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp48"
             xmlns:prism="http://prismlibrary.com/">    
</prism:PrismApplication>

//App.xaml.cs
using BookProject;
using CarProject;
using HouseProject;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using StudentProject;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApp48
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);
            moduleCatalog.AddModule<BookProjectModule>();
            moduleCatalog.AddModule<CarProjectModule>();
            moduleCatalog.AddModule<HouseProjectModule>();
            moduleCatalog.AddModule<StudentProjectModule>();
        }
    }

}


//BookProjectModule
//BookView.xaml
<UserControl x:Class="BookProject.BookView"
        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:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:local="clr-namespace:BookProject"
        mc:Ignorable="d" 
        Height="450" Width="800">
    <Grid>
        <ListBox ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 VirtualizingPanel.IsVirtualizing="True"
                 VirtualizingPanel.VirtualizationMode="Recycling"
                 VirtualizingPanel.CacheLength="2,2"
                 VirtualizingPanel.CacheLengthUnit="Item">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="FontSize" Value="30"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="FontSize" Value="50"/>
                                        <Setter Property="Foreground" Value="Red"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Id}"/>
                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>
                        <TextBlock Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="3" Text="{Binding Title}"/>
                        <TextBlock Grid.Row="1" Grid.ColumnSpan="3" Text="{Binding ISBN}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

//BookViewModel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Prism.DryIoc;
using Prism.Mvvm;

namespace BookProject
{
    public class BookViewModel:BindableBase
    {
        public BookViewModel()
        {
            BooksCollection = new ObservableCollection<Book>();
            for(int i=1;i<10001;i++)
            {
                BooksCollection.Add(new Book()
                {
                    Id = i,
                    Name = $"Name_{i}",
                    Title = $"Title_{i}",
                    ISBN = $"ISBN_{i}_{Guid.NewGuid():N}"
                });
            }
        }

        private string bookTitle="Book Project";
        public string BookTitle
        {
            get
            {
                return bookTitle; 
            }
            set
            {
                SetProperty(ref bookTitle, value);
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                SetProperty(ref booksCollection, value);
            }
        }
    }
}


//BookProjectModule
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace BookProject
{
    public class BookProjectModule : IModule
    {
        IRegionManager regionManager;
        public BookProjectModule(IRegionManager regionManagerValue)
        {
            regionManager = regionManagerValue;
        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewA"].Add(containerProvider.Resolve<BookView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<BookView>();
            containerRegistry.Register<BookProjectModule>();
        }         
    }
}


//Book.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace BookProject
{
    public class Book
    {
        public int Id { get; set;  }
        public string Name { get; set;  }
        public string ISBN { get; set;  }
        public string Title { get; set;  }
    }
}


//CarView.xaml
<UserControl x:Class="CarProject.CarView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CarProject"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <ListBox ItemsSource="{Binding CarsCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                    VirtualizingPanel.IsVirtualizing="True"
                    VirtualizingPanel.VirtualizationMode="Recycling"
                    VirtualizingPanel.CacheLength="2,2"
                    VirtualizingPanel.CacheLengthUnit="Item">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding ImgSource}"
                           RenderOptions.BitmapScalingMode="HighQuality"
                           Stretch="Uniform"
                           Height="{Binding DataContext.ImgHeight,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                           Width="{Binding DataContext.ImgWidth,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

//CarViewModel.cs
using Prism.Mvvm;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CarProject
{
    public class CarViewModel : BindableBase
    {
        private ConcurrentDictionary<string, ImageSource> imgCache = new ConcurrentDictionary<string, ImageSource>();
        public CarViewModel()
        {
            var win=Application.Current.MainWindow;
            win.Loaded += (s, e) =>
            {
                if (win != null)
                {
                    ImgHeight = win.ActualHeight / 2;
                    ImgWidth = win.ActualWidth / 2;
                }
            };
            
            CarsCollection = new ObservableCollection<CarModel>();
            LoadImagesFromResources();
        }

        private void LoadImagesFromResources()
        {
            // 1. Get the CarProject assembly
            Assembly carAssembly = Assembly.GetExecutingAssembly();
            string assemblyName = carAssembly.GetName().Name;

            // 2. Access the WPF resource dictionary (.g.resources)
            using (Stream resourceStream = carAssembly.GetManifestResourceStream($"{assemblyName}.g.resources"))
            {
                if (resourceStream == null)
                {
                    System.Diagnostics.Debug.WriteLine("Failed to open .g.resources stream!");
                    return;
                }

                // 3. Read the resource dictionary
                using (ResourceReader reader = new ResourceReader(resourceStream))
                {
                    foreach (DictionaryEntry entry in reader)
                    {                         
                        string resourceKey = entry.Key.ToString();
                        System.Diagnostics.Debug.WriteLine($"Found resource key: {Path.GetFullPath(resourceKey)}");

                        // 4. Filter for image files (png/jpg/jpeg)
                        if (resourceKey.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                            resourceKey.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
                            resourceKey.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
                        {
                            // 5. Load the image from the resource entry
                            if (entry.Value is Stream imageStream)
                            {
                                var imgSource = GetImgSourceViaStream(imageStream, resourceKey);

                                // 6. Add to your collection
                                CarsCollection.Add(new CarModel
                                {
                                    ImgSource = imgSource,
                                    ImgName = Path.GetFullPath(resourceKey)
                                });
                            }
                        }
                    }
                }
            }
        }
        
        private ImageSource GetImgSourceViaStream(Stream streamValue, string imgUrl)
        {
            if (imgCache.TryGetValue(imgUrl, out var img))
            {
                return img;
            }

            BitmapImage bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.StreamSource = streamValue;
            bmi.CreateOptions = BitmapCreateOptions.DelayCreation;
            bmi.CacheOption = BitmapCacheOption.OnDemand;
            bmi.EndInit();
            bmi.Freeze();
            imgCache[imgUrl] = bmi;
            return bmi;
        }
       
        private ObservableCollection<CarModel> carsCollection;
        public ObservableCollection<CarModel> CarsCollection
        {
            get
            {
                return carsCollection;
            }
            set
            {
                SetProperty(ref carsCollection, value);
            }
        }

        private double imgWidth;
        public double ImgWidth
        {
            get
            {
                return imgWidth;
            }
            set
            {
                SetProperty(ref imgWidth, value);
            }
        }

        private double imgHeight;
        public double ImgHeight
        {
            get
            {
                return imgHeight;
            }
            set
            {
                SetProperty(ref imgHeight, value);
            }
        }
    }
}

//CarProjectModule.cs
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace CarProject
{
    public class CarProjectModule : IModule
    {
        IRegionManager regionManager;
        public CarProjectModule(IRegionManager regionManagervalue)
        {
            regionManager=regionManagervalue;
        }
        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewB"].Add(containerProvider.Resolve<CarView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<CarView>();
        }
    }
}

//using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;

namespace CarProject
{
    public class CarModel
    {
      public ImageSource ImgSource { get; set; }
      public string ImgName { get; set;  }
    }
}


//HouseViewProject
//HouseView.xaml
<UserControl x:Class="HouseProject.HouseView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:HouseProject"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <ListBox ItemsSource="{Binding HousesCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                     VirtualizingPanel.IsVirtualizing="True"
                     VirtualizingPanel.VirtualizationMode="Recycling"
                     VirtualizingPanel.CacheLengthUnit="Item"
                     VirtualizingPanel.CacheLength="2,2">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid
                          Width="{Binding DataContext.GridWidth,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                          Height="{Binding DataContext.GridHeight,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="FontSize" Value="30"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="FontSize" Value="50"/>
                                        <Setter Property="Foreground" Value="Red"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>
                        <TextBlock Text="{Binding Name}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>


//HouseViewModel.cs
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;

namespace HouseProject
{
    public class HouseViewModel : BindableBase
    {
        public HouseViewModel()
        {
            InitHouseCollection();
            var win=Application.Current.MainWindow;
            if(win!=null)
            {
                win.Loaded += (s, e) =>
                {
                    GridHeight = win.ActualHeight / 4;
                    GridWidth = win.ActualWidth / 2;
                };
            }
        }

        private void InitHouseCollection()
        {
            HousesCollection = new ObservableCollection<HouseModel>();
            for (int i = 1; i < 1001; i++)
            {
                HousesCollection.Add(new HouseModel()
                {
                    Name = $"House_{i}_{Guid.NewGuid():N}"
                });
            }
        }

        private ObservableCollection<HouseModel> housesCollection;
        public ObservableCollection<HouseModel> HousesCollection
        {
            get
            {
                return housesCollection;
            }
            set
            {
                SetProperty(ref housesCollection, value);
            }
        }

        private double gridHeight;
        public double GridHeight
        {
            get
            {
                return gridHeight;
            }
            set
            {
                SetProperty(ref gridHeight, value);
            }
        }

        private double gridWidth;
        public double GridWidth
        {
            get
            {
                return gridWidth;
            }
            set
            {
                SetProperty(ref gridWidth, value);
            }
        }
    }
}

//HouseProjectModule.cs
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace HouseProject
{
    public class HouseProjectModule : IModule
    {
        IRegionManager regionManager;
        public HouseProjectModule(IRegionManager regionManagerValue)
        {
            regionManager = regionManagerValue;
        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewC"].Add(containerProvider.Resolve<HouseView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<HouseView>();
        }
    }
}

//StudentProjectModule
//StudentView.xaml
<UserControl x:Class="StudentProject.StudentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:StudentProject"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <ListBox ItemsSource="{Binding StudentCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                     VirtualizingPanel.IsVirtualizing="True"
                     VirtualizingPanel.VirtualizationMode="Recycling"
                     VirtualizingPanel.CacheLengthUnit="Item"
                     VirtualizingPanel.CacheLength="2,2"
                     >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding DataContext.GridWidth,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                          Height="{Binding DataContext.GridHeight,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
                        <Grid.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="FontSize" Value="30"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="FontSize" Value="50"/>
                                        <Setter Property="Foreground" Value="Red"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="0"/>
                        <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
                        <TextBlock Text="{Binding Guid}" Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

//StudentViewModel
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;

namespace StudentProject
{
    public class StudentViewModel:BindableBase
    {
        public StudentViewModel()
        {
            InitStudentCollection();
            var win=Application.Current.MainWindow;
            if(win!=null)
            {
                win.Loaded += (s, e) =>
                {
                    GridHeight = win.ActualHeight / 4;
                    GridWidth = win.ActualWidth / 2;
                };
            }
        }

        private void InitStudentCollection()
        {
            StudentCollection = new ObservableCollection<StudentModel>();
            for(int i=1;i<1001;i++)
            {
                StudentCollection.Add(new StudentModel() 
                { 
                    Id=i,
                    Name=$"Name_{i}",
                    Guid=$"{Guid.NewGuid():N}"
                });
            }
        }

        private double gridWidth;
        public double GridWidth
        {
            get
            {
                return gridWidth; 
            }
            set
            {
                SetProperty(ref gridWidth, value);
            }
        }

        private double gridHeight;
        public double GridHeight
        {
            get
            {
                return gridHeight;
            }
            set
            {
                SetProperty(ref gridHeight, value);
            }
        }

        private ObservableCollection<StudentModel> studentCollection;
        public ObservableCollection<StudentModel> StudentCollection
        {
            get
            {
                return studentCollection; 
            }
            set
            {
                SetProperty(ref studentCollection,value);
            }
        }
    }
}


//StudentProjectModule.cs
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Text;

namespace StudentProject
{
    public class StudentProjectModule:IModule
    {
        IRegionManager regionManager;
        public StudentProjectModule(IRegionManager regionManagerValue)
        {
            regionManager = regionManagerValue;
        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            regionManager.Regions["ViewD"].Add(containerProvider.Resolve<StudentView>());
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<StudentView>();
        }
    }
}


//StudentModel
using System;
using System.Collections.Generic;
using System.Text;

namespace StudentProject
{
    public class StudentModel
    {
        public int Id { get; set; }
        public string Name { get; set;  }        
        public string Guid { get; set; }
    }
}

 

 

 

 

 

In main project,add project reference

image

 Then,we can add sub modules in main project

using BookProject;
using CarProject;
using HouseProject;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using StudentProject;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApp48
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);
            moduleCatalog.AddModule<BookProjectModule>();
            moduleCatalog.AddModule<CarProjectModule>();
            moduleCatalog.AddModule<HouseProjectModule>();
            moduleCatalog.AddModule<StudentProjectModule>();
        }
    }

}

 

posted @ 2026-01-03 22:01  FredGrit  阅读(10)  评论(0)    收藏  举报