代码之家  ›  专栏  ›  技术社区  ›  Dinsdale

将容器注册类型与Shell窗口标题链接

  •  1
  • Dinsdale  · 技术社区  · 7 年前

    我有一个Prims MVVM应用程序,它使用Bootstrapper类启动应用程序和主窗口。Bootstrapper被折旧了,所以我把它换成了Prismap应用程序。我添加了一个类,该类包装串行通信,并公开在串行类状态更改(例如连接、断开等)时触发的事件。要实例化此组件,我将使用RegisterSingleton:

    public partial class App : PrismApplication
    {
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {            
            containerRegistry.RegisterSingleton<IMessages, MessageBuilder>();
            containerRegistry.RegisterSingleton<AbstractLibSerial, LibSerialVariant1>();
            containerRegistry.RegisterSingleton(typeof(SettingsModel));
        }
    
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);            
    
            moduleCatalog.AddModule(typeof(MainModule.MainModule));
            moduleCatalog.AddModule(typeof(SettingsModule.SettingsModule));            
        }
    
        protected override Window CreateShell()
        {
            return (Window)new Shell();    
        }
    }
    

    Shell.xaml:

    <Window x:Class="VibeOR.Desktop.Shell"
        xmlns:prism="http://www.codeplex.com/prism"
        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:VibeOR.Desktop"
        mc:Ignorable="d"
        Title="Vibe Base Station" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    
        <ContentControl Name="MainRegion" prism:RegionManager.RegionName="MainRegion"/>  
    

    任何输入都是巨大的。谢谢

    2 回复  |  直到 7 年前
        1
  •  1
  •   Haukinger    7 年前

    为了实例化这个组件,我使用 RegisterSingleton

    创建一个实例。

    如果希望创建并注册单例,可以注册实例(实例始终为单例):

    _container.RegisterInstance<IInterface>( _container.Resolve<Implementation>() );
    

    Implementation 尚未注册(它们可能来自其他模块)。

        2
  •  0
  •   Dinsdale    7 年前

    public partial class App : PrismApplication
    {
        private AbstractLibSerial _lib;
    
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {            
            containerRegistry.RegisterSingleton<IMessageBuilder, MessageBuilderVariant1>();
            containerRegistry.RegisterSingleton<AbstractLibSerial, LibSerialVariant1>();
            containerRegistry.RegisterSingleton(typeof(SettingsModel));
            var _container = containerRegistry.GetContainer();
            _lib = _container.Resolve<LibSerialVariant1>();
            containerRegistry.RegisterInstance<AbstractLibSerial>(_lib);
        }
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);            
    
    
            moduleCatalog.AddModule(typeof(MainModule.MainModule));
            moduleCatalog.AddModule(typeof(SettingsModule.SettingsModule));            
        }
    
        protected override void InitializeShell(Window shell)
        {
            base.InitializeShell(shell);
            _lib.SerialEvent += ((Shell)shell).OnSerialEvent;
        }
        protected override Window CreateShell()
        {
            return (Window)new Shell();
        }
    
    }