WPF Prism.Wpf Prism.DryIOC integrate modules into MainWindow, invoke other project as dll

Install-Package Prism.DryIOC;
Install-Package Prism.Wpf;

 

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


//App.xaml.cs
using DGView;
using ImageView;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WpfApp26.Services;
using WpfApp26.ViewModels;
using WpfApp26.Views;

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

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<ITimeGuidService, TimeGuidService>();
            containerRegistry.RegisterForNavigation<HeaderView, HeaderViewModel>("headerView");
            containerRegistry.RegisterForNavigation<MainWindow, MainWindowViewModel>("MainView");
        }

        protected override void OnInitialized()
        {
            var mainWind = Container.Resolve<MainWindow>();
            regionManager = Container.Resolve<IRegionManager>();
            mainWind.Show();
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);
            moduleCatalog.AddModule<DgViewModule>();
            moduleCatalog.AddModule<ImageModule>();
        }
    }

}


//D:\C\WpfApp26\DGView\DgViewModule.cs
using DGView.Services;
using DGView.ViewModels;
using DGView.Views;
using System;
using System.Collections.Generic;
using System.Text;

namespace DGView
{
    public class DgViewModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();

            // Navigate to DgView in the DgRegion
            regionManager.RequestNavigate("LeftRegion", "dgView");
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<IIDService,IDService>();
            containerRegistry.Register<INameService,NameService>();
            containerRegistry.Register<IISBNService,ISBNService>();
            containerRegistry.RegisterForNavigation<DgView, DgViewModel>("dgView");
        }
    }
}


//D:\C\WpfApp26\ImageView\ImageModule.cs
using ImageView.ViewModels;
using ImageView.Views;
using System;
using System.Collections.Generic;
using System.Text;

namespace ImageView
{
    public class ImageModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            //var regionManager= containerProvider.Resolve<IRegionManager>();
            //regionManager.RequestNavigate("RightViewRegion", "imgView");
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<ImgView, ImgViewModel>("imgView");
        }
    }
}

 

 

image

 

 

 

 

 

 

 

 

image

 

image

 

 

 

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


//App.xaml.cs
using DGView;
using ImageView;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WpfApp26.Services;
using WpfApp26.ViewModels;
using WpfApp26.Views;

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

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<ITimeGuidService, TimeGuidService>();
            containerRegistry.RegisterForNavigation<HeaderView, HeaderViewModel>("headerView");
            containerRegistry.RegisterForNavigation<MainWindow, MainWindowViewModel>("MainView");
        }

        protected override void OnInitialized()
        {
            var mainWind = Container.Resolve<MainWindow>();
            regionManager = Container.Resolve<IRegionManager>();
            mainWind.Show();
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);
            moduleCatalog.AddModule<DgViewModule>();
            moduleCatalog.AddModule<ImageModule>();
        }
    }

}


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

namespace WpfApp26.Services
{
    public interface ITimeGuidService
    {
        string GetTimeGuid();
    }
    public class TimeGuidService : ITimeGuidService
    {
        int idx = 0;

        public string GetTimeGuid()
        {
            return $"{++idx}_{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")}_{Guid.NewGuid():N}";
        }
    }
}
using Prism.Common;
using System;
using System.Collections.Generic;
using System.Text;
using WpfApp26.Services;


namespace WpfApp26.ViewModels
{
    public class HeaderViewModel:BindableBase
    {
        ITimeGuidService timeGuidService;
        public HeaderViewModel(ITimeGuidService timeGuidServiceValue)
        {
            timeGuidService = timeGuidServiceValue;
            HeaderStr = timeGuidService.GetTimeGuid();
            Task.Run(() =>
            {
                InitTimer();
            });
        }

        private void InitTimer()
        {
            System.Timers.Timer tmr= new System.Timers.Timer();
            tmr.Interval = 1000;
            tmr.Elapsed += Tmr_Elapsed;
            tmr.Start();
        }

        private void Tmr_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            HeaderStr = timeGuidService.GetTimeGuid();
        }

        private string headerStr;
        public string HeaderStr
        {
            get
            {
                return headerStr;
            }
            set
            {
                SetProperty(ref headerStr, value);
            }
        }
    }
}
<UserControl x:Class="WpfApp26.Views.HeaderView"
             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:WpfApp26.Views"
             xmlns:prism="http://prismlibrary.com/"
             mc:Ignorable="d"
             Height="100"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <TextBlock Text="{Binding HeaderStr}">
            <TextBlock.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="FontSize" Value="50"/>
                    <Setter Property="HorizontalAlignment" Value="Left"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="FontSize" Value="50"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Resources>
        </TextBlock>
    </Grid>
</UserControl>
using DGView.Views;
using ImageView.Views;
using Prism;
using Prism.Navigation.Regions;
using System;
using System.Collections.Generic;
using System.Text;
using WpfApp26.Views;

namespace WpfApp26.ViewModels
{
    public class MainWindowViewModel:BindableBase
    {
        IRegionManager regionManager;
        IContainerProvider containerProvider;
        private HeaderView headerView;
        public MainWindowViewModel(IRegionManager regionManagerValue, IContainerProvider containerProviderValue)
        {
            regionManager = regionManagerValue;
            containerProvider = containerProviderValue;
        }

        public void NavigateToView(string viewName="headerView")
        {
            //regionManager.RequestNavigate("HeaderRegion", viewName);
            headerView=containerProvider.Resolve<HeaderView>();
            HeaderViewContent=headerView;
            var dgView = containerProvider.Resolve<DgView>();
            LeftViewContent= dgView;

            var imgView = containerProvider.Resolve<ImgView>();
            RightViewContent= imgView;
        }

        private object headerViewContent;
        public object HeaderViewContent
        {
            get
            {
                return headerViewContent;
            }
            set
            {
                SetProperty(ref headerViewContent, value);
            }
        }

        private object leftViewContent;
        public object LeftViewContent
        {
            get
            {
                return leftViewContent;
            }
            set
            {
                SetProperty(ref leftViewContent, value);
            }
        }

        private object rightViewContent;
        public object RightViewContent
        {
            get
            {
                return rightViewContent;
            }
            set
            {
                SetProperty(ref rightViewContent, value);
            }
        }

    }
}

<Window x:Class="WpfApp26.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:WpfApp26"
        xmlns:prism="http://prismlibrary.com/"        
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow"
        VerticalAlignment="Stretch"
        HorizontalAlignment="Stretch"
        prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ContentControl Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" 
                        Content="{Binding HeaderViewContent}"/>
        <ContentControl Grid.Row="1" Grid.Column="0"
                        Content="{Binding LeftViewContent}"/>
        <ContentControl Grid.Row="1" Grid.Column="1"
                        Content="{Binding RightViewContent}"/>
    </Grid>
</Window>


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

namespace DGView.Model
{
    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ISBN {  get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace DGView.Services
{
    public interface IIDService
    {
        int GetID();
    }
    public class IDService : IIDService
    {
        int idx = 0;
        public int GetID()
        {
            return Interlocked.Increment(ref idx);
        }
    }

    public interface INameService
    {
        public string GetName();
    }

    public class NameService : INameService
    {
        int idx = 0;
        public string GetName()
        {
            return $"Name_{Interlocked.Increment(ref idx)}";
        }
    }


    public interface IISBNService
    {
        string GetISBN();
    }

    public class ISBNService : IISBNService
    {
        int idx = 0;

        public string GetISBN()
        {
            return $"ISBN_{Interlocked.Increment(ref idx)}_{Guid.NewGuid():N}";
        }
    }
}
using DGView.Model;
using DGView.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace DGView.ViewModels
{
    public class DgViewModel : BindableBase
    {
        IIDService idService;
        INameService nameService;
        IISBNService isbnService;

        public DgViewModel(IDService idServiceValue, INameService nameServiceValue, IISBNService isbnServiceValue)
        {
            idService = idServiceValue;
            nameService = nameServiceValue;
            isbnService = isbnServiceValue;
            InitBooksCollection();
        }

        public void InitBooksCollection()
        {
            BooksCollection=new ObservableCollection<Book>();
            for(int i=1;i<10001;i++)
            {
                BooksCollection.Add(new Book()
                {
                    Id = idService.GetID(),
                    Name = nameService.GetName(),
                    ISBN = isbnService.GetISBN(),
                    Title = $"Title_{i}",
                    Topic = $"Topic_{i}"
                });
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                SetProperty(ref booksCollection, value);
            }
        }
    }
}
<UserControl x:Class="DGView.Views.DgView"
             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:DGView.Views"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             VerticalAlignment="Stretch"
             HorizontalAlignment="Stretch"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Background="Cyan">
        <DataGrid ItemsSource="{Binding BooksCollection,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
                  FontSize="30"
                      VirtualizingPanel.IsVirtualizing="True"
                      VirtualizingPanel.VirtualizationMode="Recycling"
                      VirtualizingPanel.CacheLength="2,2"
                      VirtualizingPanel.CacheLengthUnit="Item"
                      ScrollViewer.IsDeferredScrollingEnabled="True"
                      ScrollViewer.CanContentScroll="True"
                      AutoGenerateColumns="True"
                      CanUserAddRows="False">
        </DataGrid>
    </Grid>
</UserControl>

using DGView.Services;
using DGView.ViewModels;
using DGView.Views;
using System;
using System.Collections.Generic;
using System.Text;

namespace DGView
{
    public class DgViewModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();

            // Navigate to DgView in the DgRegion
            regionManager.RequestNavigate("LeftRegion", "dgView");
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<IIDService,IDService>();
            containerRegistry.Register<INameService,NameService>();
            containerRegistry.Register<IISBNService,ISBNService>();
            containerRegistry.RegisterForNavigation<DgView, DgViewModel>("dgView");
        }
    }
}




//ImgViewModule
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageView.ViewModels
{
    public class ImgViewModel : BindableBase
    {
        List<string> imgsList;
        int imgsCount = 0;
        public ImgViewModel()
        {          
            string imgDir = @"Images";
            if(Directory.Exists(imgDir))
            {
                imgsList = new List<string>(Directory.GetFiles(imgDir));
                imgsCount = imgsList.Count;
                ImgUrl=imgsList[0];
                ImgSource = GetImgSourceViaUrl(imgsList[0]);
            }
            Task.Run(() =>
            {
                InitTimers();
            });            
        }

        private void InitTimers()
        {
            System.Timers.Timer tmr=new System.Timers.Timer();
            tmr.Elapsed += Tmr_Elapsed;
            tmr.Interval = 1000;
            tmr.Start();
        }

        int idx = 0;
        private void Tmr_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            ImgUrl = imgsList[++idx % imgsCount];
            ImgSource = GetImgSourceViaUrl(ImgUrl);
        }

        private string imgUrl;
        public string ImgUrl
        {
            get
            {
                return imgUrl; 
            }
            set
            {
                SetProperty(ref imgUrl, value);
            }
        }

        private ImageSource imgSource;
        public ImageSource ImgSource
        {
            get
            {
                return imgSource;
            }
            set
            {
                SetProperty(ref imgSource, value);
            }
        }

        private ImageSource GetImgSourceViaUrl(string imgUrl)
        {
            BitmapImage bmi = new BitmapImage();
            if(!File.Exists(imgUrl))
            {
                return bmi;
            }

            bmi.BeginInit();
            bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute);
            bmi.EndInit();
            bmi.Freeze();
            return bmi;
        }
    }
}

<UserControl x:Class="ImageView.Views.ImgView"
             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:ImageView.Views"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             VerticalAlignment="Stretch"
             HorizontalAlignment="Stretch"
             >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Image Grid.Row="0"
               Source="{Binding ImgSource,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   Stretch="Fill"/>
        <TextBlock Text="{Binding ImgUrl}"
                   Grid.Row="1"
                   FontSize="30"/>
    </Grid>
</UserControl>

using ImageView.ViewModels;
using ImageView.Views;
using System;
using System.Collections.Generic;
using System.Text;

namespace ImageView
{
    public class ImageModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            //var regionManager= containerProvider.Resolve<IRegionManager>();
            //regionManager.RequestNavigate("RightViewRegion", "imgView");
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<ImgView, ImgViewModel>("imgView");
        }
    }
}

 

 

 

 

image

 

posted @ 2025-10-07 17:24  FredGrit  阅读(6)  评论(0)    收藏  举报