JohnSon:ModuleManager动态加载模块

[Export(typeof(VMShell))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class VMShell : NotificationObject
    {
        IEventAggregator eventAggregator;
        Lazy<IModuleManager> lazyModuleManager;
        IModuleManager moduleManager = null;

        [ImportingConstructor]
        public VMShell(IEventAggregator agg, Lazy<IModuleManager> module)
        {
            eventAggregator = agg;
            lazyModuleManager = module;

            ShowLoginView();
        }

        private void ShowLoginView()
        {
            //初始化ModuleManager,加载事件等
            InitLoginManager();
            //根据模块名取得模块信息
            ModuleCatalog moduleCatalog = ServiceLocator.Current.GetInstance<ModuleCatalog>();
            if (moduleCatalog == null) return;
            ModuleInfo moduleInfo = moduleCatalog.Modules.FirstOrDefault(p => p.ModuleName == "ModuleLogin");
            if (moduleInfo == null) return;
            if (moduleInfo.State == ModuleState.Initialized)
            {
                //显示当前主视图
                ShowLogin();
            }
            else if (moduleManager != null)
            {
                moduleManager.LoadModule("ModuleLogin");
            }
            string status = moduleInfo.State.ToString();
        }

        private void InitLoginManager()
        {
            if (!lazyModuleManager.IsValueCreated)
            {
                moduleManager = lazyModuleManager.Value;
                moduleManager.ModuleDownloadProgressChanged += new EventHandler<ModuleDownloadProgressChangedEventArgs>(LoginModuleProgressChanged);
                moduleManager.LoadModuleCompleted += new EventHandler<LoadModuleCompletedEventArgs>(LoadModuleCompleted);
            }
        }

        void LoginModuleProgressChanged(object sender, ModuleDownloadProgressChangedEventArgs e)
        {
            //ModuleLoadedPercent = e.ProgressPercentage;
            //if (ModuleLoadedPercent > 0 && ModuleLoadedPercent < 100)
            //    IsBusy = true;
            //else
            //    IsBusy = false;
        }

        void LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                ShowLogin();
            }
        }

        private void ShowLogin()
        {
            UserControl loginView = ServiceLocator.Current.GetInstance<UserControl>("ViewLogin");
            if (loginView != null)
            {
                eventAggregator.GetEvent<ModuleEvent>().Publish(loginView);
            }
        }
    }

转载于:https://www.cnblogs.com/iwangjun/archive/2012/03/08/2385812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值